Jump to content

Can't get player(ped) on server side function


Tails

Recommended Posts

Almost feel ashamed of posting this but I just can't figure out this little function:

function respawnInactiveVehicles(thePlayer) 
    local vehicles = getElementsByType ("vehicle") 
    local inVeh = getPedOccupiedVehicle(thePlayer) 
    for _, vehicle in ipairs(vehicles) do 
        if not inVeh then 
            respawnVehicle(vehicle) 
        end 
    end 
end 
setTimer(respawnInactiveVehicles,5000,0) 

I'm getting a bad argument on line 3: Expected ped at argument 1 got nil. Server side script.

Any help is appreciated

Link to comment
function respawnInactiveVehicles() 
    for _, vehicle in ipairs(getElementsByType("vehicle")) do 
        if not getVehicleOccupant(vehicle) then 
            respawnVehicle(vehicle) 
        end 
    end 
end 
setTimer(respawnInactiveVehicles,5000,0) 

EDIT: This code isn't ideal as, for example, vehicles with passengers and no driver will respawn. However it fixes the issue you were having.

Link to comment

Cheers. That indeed fixed the issue I was having but the passengers thing is an issue. This is the original code in my script:

function respawnExplodedVehicle() 
    setTimer(respawnVehicle, 20000, 1, source) 
end 
addEventHandler("onVehicleExplode", root, respawnExplodedVehicle) 
  
function respawnInactiveVehicles() 
    local vehicles = getElementsByType ("vehicle") 
    for _, vehicle in ipairs(vehicles) do 
        if (vehicles) then 
            toggleVehicleRespawn(vehicle, true) 
            setVehicleIdleRespawnDelay(vehicle, 120000) 
        end 
    end 
end 
addEventHandler ("onResourceStart", root, respawnInactiveVehicles) 

The problem here is that the timer in the first function doesn't work properly. Exploded vehicles respawn after 5-10 seconds. So I was looking for a solution.

Any ideas?

Link to comment
  
local SecondsDelayForSpawn = 1 -- 1 second 
    function respawnExplodedVehicle() 
  
    setTimer(respawnVehicle, SecondsDelayForSpawn*1000, 1, source) 
  
end 
  
addEventHandler("onVehicleExplode", root, respawnExplodedVehicle) 
  
  
  
function respawnInactiveVehicles() 
  
    local vehicles = getElementsByType ("vehicle") 
  
    for _, vehicle in ipairs(vehicles) do 
  
        if (vehicles) then 
  
            toggleVehicleRespawn(vehicle, true) 
  
            setVehicleIdleRespawnDelay(vehicle, 120000) 
  
        end 
  
    end 
  
end 
  
addEventHandler ("onResourceStart", root, respawnInactiveVehicles 
  

Link to comment
  • Moderators
local SecondsDelayForSpawn = 1 -- 1 second 
addEventHandler("onVehicleExplode", root,  
function () 
    toggleVehicleRespawn(source,false) 
    setTimer(function (vehicle) 
        if isElement(vehicle) then 
            respawnVehicle(vehicle) 
            toggleVehicleRespawn(vehicle,true) 
        end 
    end,SecondsDelayForSpawn*1000, 1,source) 
end) 
  
addEventHandler ("onResourceStart", resourceRoot,   
function() 
    local vehicles = getElementsByType ("vehicle") 
    for i=1,#vehicles do 
        local vehicle = vehicles[i] 
        toggleVehicleRespawn(vehicle, true) 
        setVehicleIdleRespawnDelay(vehicle, 120000) 
    end 
end) 

Link to comment

In the time that you disable the toggleVehicleRespawn, doesn't that screw up the setVehicleIdleRespawnDelay from the second function? There still seems to be an issue. At one point I had my inactive vehicle timer set to 10 and they would spawn after 7 seconds. Also if I enter a vehicle and move it only a few cm's it will not get respawned for some reason. Any clues?

Link to comment
  • Moderators
In the time that you disable the toggleVehicleRespawn, doesn't that screw up the setVehicleIdleRespawnDelay from the second function?

Afaik not, it is only toggle, but if you want to be sure you can set setVehicleIdleRespawnDelay again.

Mta has it's own respawn check system, I have no idea how that works and I can't take a look in that.

Also keep in mind that next to setVehicleIdleRespawnDelay, there is also:

https://wiki.multitheftauto.com/wiki/Se ... spawnDelay

After the setVehicleIdleRespawnDelay has been expired, then there is also this delay.

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