Jump to content

[Ajuda] math.random table


Recommended Posts

Salve galera!, estou com um problema que anda me afetando muito, e gostaria de entender o porque acontece isso, eu tenho uma tabela dentro de outra tabela, e quero pegar os elementos da tabela dentro de uma tabela randomicamente.

Mais ou menos isso:

 

Boxs = {
    Marker = {
        Start = { 
            {-714.84930, 961.21680, 12.23629} 
        },
        Positions = { 
            {-709.06329345703, 951.41162109375, 11.969321250916},
            {-715.99768066406, 974.80853271484, 11.71480178833}
        },
        Create = {}
    },
    Vehicle = {}
}

Positions = {
    {-709.06329345703, 951.41162109375, 11.969321250916},            
    {-715.99768066406, 974.80853271484, 11.71480178833}
}

addEventHandler("onMarkerHit", root,
    function (Jogador)
        if (source == Boxs.Marker.Create[1]) then
            if (getElementType(Jogador) == "player") then
                if (isElement(Boxs.Vehicle[Jogador])) then
                    destroyElement(Boxs.Vehicle[Jogador]);
                end
                local x, y, z = getElementPosition(Jogador)
                Boxs.Vehicle[Jogador] = createVehicle(411, x, y, z);
                warpPedIntoVehicle(Jogador, Boxs.Vehicle[Jogador]);
                Boxs.Marker.Create[2] = createMarker(unpack(Boxs.Marker.Positions[math.random(1, #Boxs.Marker.Positions)]), "cylinder", 1, 255, 0, 0, 255);
            end
        end
    end
)

for i, v in ipairs(Boxs.Marker.Start) do
    Boxs.Marker.Create[1] = createMarker(v[1], v[2], v[3], "cylinder", 1, 255, 0, 0);
end

 

Se alguém poder dar uma ajuda, ficarei muito grato :D

Edited by DNL291
Link to comment
  • DNL291 featured and unfeatured this topic
  • Moderators

Tente isto:

addEventHandler("onMarkerHit", resourceRoot,
    function (Jogador)
        if (source == Boxs.Marker.Create[1]) then
            if (getElementType(Jogador) == "player") then
                if (isElement(Boxs.Vehicle[Jogador])) then
                    destroyElement(Boxs.Vehicle[Jogador]);
                end
                local x, y, z = getElementPosition(Jogador)
                local mx, my, mz = unpack( Boxs.Marker.Positions[ math.random( #Boxs.Marker.Positions ) ] )
                Boxs.Vehicle[Jogador] = createVehicle(411, x, y, z);
                warpPedIntoVehicle(Jogador, Boxs.Vehicle[Jogador]);
                Boxs.Marker.Create[2] = createMarker(mx, my, mz, "cylinder", 1, 255, 0, 0, 255);
            end
        end
    end
)

 

Só defini variável pras posições da marca, não achei outro erro. Você tava tentando usar unpack direto dentro da função, isso não funciona (pelo que me lembro).

Se não funcionar, mostre o erro do debug.

Link to comment

Só para complementar, Isso ocorre porque a linguagem pega apenas o primeiro argumento passado de funções executadas dentro de outras funções.

Ou seja, Nessa linha:

Boxs.Marker.Create[2] = createMarker(unpack(Boxs.Marker.Positions[math.random(1, #Boxs.Marker.Positions)]), "cylinder", 1, 255, 0, 0, 255); 

A linguagem só vai pegar o primeiro argumento retornado da função unpack, Assim ficando:

Boxs.Marker.Create[2] = createMarker(POSICAO_X, "cylinder", 1, 255, 0, 0, 255); 

Por isso o erro ocorre, A solução é fazer como o DNL fez, criar variáveis com os valores retornados pela função unpack e depois usar essas variáveis na função createMarker.

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...