Jump to content

Vehicle Spawn problem


MadMan

Recommended Posts

Hello, my problem is that i spawn a car,but if someone else spawns another car, then my car dissapears

server:

addEvent("onPlayerRequestVehicle",true); 
addEventHandler("onPlayerRequestVehicle",getRootElement(), 
function(carID) 
    if getElementType(source) == "player" then 
    if isElement(vehicle) then destroyElement(vehicle) end 
    for i,v in ipairs (disallowedVehicles) do 
        if (v[1] == carID) then 
            outputChatBox("This is disallowed vehicle!",source,255,0,0) 
            return false 
        end 
    end 
    playerX,playerY,playerZ = getElementPosition(source) 
    vehicle = createVehicle(carID,playerX + 2,playerY +2, playerZ) 
end 
end 
) 

Link to comment
  
addEvent("onPlayerRequestVehicle",true); 
addEventHandler("onPlayerRequestVehicle",getRootElement(), 
function(carID) 
    if getElementType(source) == "player" then 
    for i,v in ipairs (disallowedVehicles) do 
        if (v[1] == carID) then 
            outputChatBox("This is disallowed vehicle!",source,255,0,0) 
            return false 
        end 
    end 
    playerX,playerY,playerZ = getElementPosition(source) 
    vehicle = createVehicle(carID,playerX + 2,playerY +2, playerZ) 
end 
end 
) 
  

Link to comment
  
addEvent("onPlayerRequestVehicle",true); 
addEventHandler("onPlayerRequestVehicle",getRootElement(), 
function(carID) 
    if getElementType(source) == "player" then 
    for i,v in ipairs (disallowedVehicles) do 
        if (v[1] == carID) then 
            outputChatBox("This is disallowed vehicle!",source,255,0,0) 
            return false 
        end 
    end 
    playerX,playerY,playerZ = getElementPosition(source) 
    vehicle = createVehicle(carID,playerX + 2,playerY +2, playerZ) 
end 
end 
) 
  

Thanks, but now there's another problem, i can spawn unlimited ammount of cars :/ how i can set a limit per player for example 2 cars per player ?

Link to comment
  
  
addEvent("onPlayerRequestVehicle",true); 
addEventHandler("onPlayerRequestVehicle",getRootElement(), 
function(carID) 
local vehicleSpawnCounter = {} 
local vehicleSpawnLimit = 2    
    if getElementType(source) == "player" then 
    for i,v in ipairs (disallowedVehicles) do 
        if (v[1] == carID) then 
            outputChatBox("This is disallowed vehicle!",source,255,0,0) 
            return false 
        end 
    end 
     if vehicleSpawnCounter[source] <= vehicleSpawnLimit then 
    playerX,playerY,playerZ = getElementPosition(source) 
    vehicle = createVehicle(carID,playerX + 2,playerY +2, playerZ) 
    vehicleSpawnCounter[source] = vehicleSpawnCounter[source] + 1 
else 
outputChatBox("Vehicle Spawn Limit Reached!",source,255,0,0) 
   end  
end 
end 
) 

Link to comment
I guess you have to set vehicleSpawnCounter[source] to 0 somewhere first.

But you can also put some debug outputs to find problems yourself.

Vehicle spawncounter is set to 0

server:

addEvent("onPlayerRequestVehicle",true); 
addEventHandler("onPlayerRequestVehicle",getRootElement(), 
function(carID) 
local disallowedVehicles = {{425},{432}} 
local vehicleSpawnCounter = {  } 
local vehicleSpawnLimit = 2   
    if getElementType(source) == "player" then 
    for i,v in ipairs (disallowedVehicles) do 
        if (v[1] == carID) then 
            outputChatBox("This is disallowed vehicle!",source,255,0,0) 
            return false 
        end 
    end 
    if vehicleSpawnCounter[source] <= vehicleSpawnLimit then --line 39 
    playerX,playerY,playerZ = getElementPosition(source) 
    vehicle = createVehicle(carID,playerX + 2,playerY +2, playerZ) 
    vehicleSpawnCounter[source] = vehicleSpawnCounter[source] + 1 
else 
outputChatBox("Vehicle Spawn Limit Reached!",getRootElement(),255,0,0) 
   end 
end 
end 
) 

Debug says line 39: attempt to compare nil with number

Link to comment

There is no line 39.. so there are either parts missing or you post the wrong script.

I also don't see where you set vehicleSpawnCounter[source] to 0.

Assuming line 39 is line 14 here, vehicleSpawnCounter[source] is nil.

Therefore, put "if not vehicleSpawnCounter[source] then vehicleSpawnCounter[source] = 0 end"

EDIT: It seems like you clear the table when ever a player requests a car.

Link to comment
  • Moderators

Here is a code using element datas. This script will destroy the oldest vehicle instead of preventing him to spawn like Anubhav did: (check the comments)

local vehicleSpawnLimit = 2 
addEvent("onPlayerRequestVehicle",true); 
addEventHandler("onPlayerRequestVehicle", root, function( carID ) 
    if getElementType(source) == "player" then 
        for i, v in ipairs (disallowedVehicles) do 
            if (v[1] == carID) then 
                outputChatBox("This is disallowed vehicle!",source,255,0,0) 
                return false 
            end 
        end 
        --Limit check here 
        local pVehicles = getELementData(source, "vehicles") -- get the player's vehicle list 
        if not pVehicles or type( pVehicles ) ~= "table" then --if it's not a table (a list) 
            pVehicles = {} --create the table (list) 
        end 
  
        local playerX, playerY, playerZ = getElementPosition(source) 
        local veh = createVehicle(carID, playerX + 2, playerY +2, playerZ) 
        if veh then 
            if #pVehicles == vehicleSpawnLimit then --if he already spawned 2 vehicles 
                destroyElement(pVehicles[1]) --destroy the oldest player's vehicle 
            end 
            table.insert(pVehicles, veh) --Add the car to the player's vehicle list 
            setElementData(source, "vehicles", pVehicles) --store the updated list 
        else 
            outputChatBox( "An error has occured when spawning the vehicle !", source, 200, 0, 0) 
        end 
    end 
end) 

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