Jump to content

Random Postion


TorNix~|nR

Recommended Posts

Hello guys, I have my event used by slothbot, it's working fine, but I want to change the postions to randomly

I tried many attemps, and I failed, can you help please?

this is the normal code

local bossDeady = {
    {1952.16, 707.50, 11.82, 90, 97, 38, 5000, 20000, 20000, "NAME"},
}

    local x, y, z, rot, skin, weapon, health, money, _, name, _, ped = unpack(bossDeady[ID])

    bossDeady[ID][12] = exports["slothbot"]:spawnBot(x, y, z+1, rot, skin, 0, 0, nil, weapon, "hunting")

but I want to use it random like this

local posTable = {
    {-1341.54, -149.02, 14.14},
    {-1586.54, -214.85, 14.14},
    {-1478.24, 12.55, 14.36}
}

    local x, y, z = unpack(posTable[math.random(#posTable)])

but I do not know how to do it with normal code, help please?

Link to comment

test my code.. use command /test

 

local position =
    {
        { 816.59997558594, -7209.2998046875, 32.799999237061 },
        { 707.20001220703, -7272.6000976563, 32.700000762939 },
        { 710.5, -7412.8999023438, 32.799999237061 },
        { 798.70001220703, -7654, 32.5 },
        { 816.09997558594, -7471.3999023438, 30 },
        { 812.40002441406, -7321.6000976563, 32.799999237061 },
    }
    
function myteleport ( thePlayer, command )
    local azar = math.random ( #position )
    local veh = getPedOccupiedVehicle(thePlayer)
        if (veh) then
            setElementPosition(veh, unpack ( position [ azar ] ) )
        else
            setElementPosition(thePlayer, unpack ( position [ azar ] ) )
        end    
            outputChatBox ( "test", root, 0, 255, 0, true )
end
addCommandHandler ( "test", myteleport  )

 

Edited by #RooTs
  • Thanks 1
Link to comment

You can only use unpack with a function that allows multiple OPTIONAL values.
Maybe this will work better for you.

local position = {
	{816.59997558594,-7209.2998046875,32.799999237061},
	{707.20001220703,-7272.6000976563,32.700000762939},
	{710.5,-7412.8999023438,32.799999237061},
	{798.70001220703,-7654,32.5},
	{816.09997558594,-7471.3999023438,30},
	{812.40002441406,-7321.6000976563,32.799999237061}
}

function getRandomPosition()
	local r = math.random(#position)
	return position[r][1],position[r][2],position[r][3]
end

function myteleport(player,command)
	local element
	local x,y,z = getRandomPosition()
	local vehicle = getPedOccupiedVehicle(player)
	if vehicle then element = vehicle else element = player end
	if isElement(element) then
		setElementPosition(element,x,y,z)
	end
end
addCommandHandler('test',myteleport)

 

Edited by ShayF
  • Thanks 1
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...