Jump to content

[help]randomize table item


raynner

Recommended Posts

Hello everyone I currently have this function that develops, and some time and I wanted to make a change most do not know it as good is simple.

local positions =
    {
        { 371, -2040, 8 },
		{ 2274, -1036, 52 },
		{ -2245, -1723, 482 },
		{ -155, -2889, 428 },
    }
	
function DisappearPlayer (NamePlayer, Admin)
   local azar = math.random ( #positions )
   local veh = getPedOccupiedVehicle(NamePlayer)
       if (veh) then
	  	  setElementPosition(veh, unpack ( positions [ azar ] ) )
	   else
		  setElementPosition(NamePlayer, unpack ( positions [ azar ] ) )
	   end
end
addEvent("random",true)
addEventHandler("random",getRootElement(),DisappearPlayer)

This teleports the player into a random position map using a table of coordinates.

Ok everything works perfectly. But I wanted it stores up the item from the table that has already been used and not used again until the last was used or the successor.

Ex

local positions =
    {
        { 1 },
		{ 2 },
		{ 3 },
		{ 4 },
    }
	
function DisappearPlayer (NamePlayer, Admin)
   local azar = math.random ( #positions )
   local veh = getPedOccupiedVehicle(NamePlayer)
       if (veh) then
	  	  setElementPosition(veh, unpack ( positions [ azar ] ) )
	   else
		  setElementPosition(NamePlayer, unpack ( positions [ azar ] ) )
	   end
end
addEvent("random",true)
addEventHandler("random",getRootElement(),DisappearPlayer)

so that happens.

3,3,2,1,3,4,4

way I wanted it occurs.

3.1.2.4 or 3.2.3.1.3

in a way that the same n repeita then!

Anyone know how to do?

Link to comment
local positions =
{
    { 371, -2040, 8 },
    { 2274, -1036, 52 },
    { -2245, -1723, 482 },
    { -155, -2889, 428 },
}

function DisappearPlayer (NamePlayer, Admin)
    local attempts = 0
    local azar = math.random ( 1,#positions )
    if isElement(position[azar][4]) then
        repeat
        azar = math.random ( 1,#positions )
        attempts = attempts + 1
        if attempts >= #positions then
            outputChatBox("All positions are taken.") -- Just a debug, dont use unless you want spam
            break
        end
        until not isElement(position[azar][4])
    end

    local veh = getPedOccupiedVehicle(NamePlayer)
    if (veh) then
        setElementPosition(veh, unpack ( positions [ azar ] ) )
        positions [ azar ] = veh -- Store the userdata in the table incase u need it for later.
    else
        setElementPosition(NamePlayer, unpack ( positions [ azar ] ) )
        positions [ azar ] = NamePlayer -- Store the userdata in the table incase u need it for later.
    end
end
addEvent("random",true)
addEventHandler("random",root,DisappearPlayer)

function resetPositions()
    for i=1,#positions do
        if isElement(positions[i][4]) then
            positions[i][4] = nil
        end
    end
end
addEvent("onGameStart",true)
addEventHandler("onGameStart",root,resetPositions)

Untested but i think it should work.

Just remember to call the onGameStart event before spawning the players.

Link to comment
Just now, loki2143 said:

I added a new function to the bottom to empty the table spawn points so players can spawn again.

call the event "onGameStart" before you call "random"

 

Oh ok thank man

I will test it soon :D

Edited by raynner
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...