Jump to content

[Help]Tables (table.sort for keys)


Recommended Posts

Hello, I was trying to make players can only spawn 5 vehicles and If they spawned the 6th one.

Then the 1st will get destroy and The Table will reset.

Here is my Script:-

vehicles = { } 
function spawnVehicle ( myPlayer ) 
if ( not vehicles[myPlayer] ) then 
        vehicles[myPlayer] = {} 
    end 
    local x,y,z = getElementPosition ( myPlayer ) 
    local car = createVehicle ( 560, x, y, z ) 
    table.insert(vehicles[myPlayer],car) 
    for k,v in pairs(vehicles[myPlayer] do 
        if (#vehicles[myPlayer] >= 5) then 
            destroyElement(vehicles[myPlayer][1]) 
            table.sort[k] 
        else 
            warpPedIntoVehicle ( myPlayer, car ) 
            outputChatBox("Vehicle Spawned",myPlayer, 0, 255, 0, true) 
            setVehicleLocked ( car, true )  
        end 
    end 
end 
addCommandHandler ( "vehCar", spawnVehicle ) 

Can You Guys Please Help Me ?

Link to comment

Oh, I didn't know that there is something like table.remove exist.

I just have to put

table.remove(vehicles[myPlayer][1]) 

in replace of table.sort ?

And Every other thing is fine ?

Edited by Guest
Link to comment
vehicles = {} 
  
function spawnVehiclee(myPlayer) 
    if (not vehicles[myPlayer]) then 
        vehicles[myPlayer] = {} 
    end 
    local x,y,z = getElementPosition(myPlayer) 
    local car = createVehicle(560, x, y, z) 
    table.insert(vehicles[myPlayer], car) 
    if (#vehicles[myPlayer] > 5) then 
        destroyElement(vehicles[myPlayer][1]) 
        table.remove(vehicles[myPlayer], 1) 
    end 
    warpPedIntoVehicle(myPlayer, car) 
    outputChatBox("Vehicle Spawned", myPlayer, 0, 255, 0, true) 
    setVehicleLocked(car, true) 
end 
addCommandHandler("vehCar", spawnVehiclee) 

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...