Tails Posted September 19, 2014 Share Posted September 19, 2014 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
Callum Posted September 19, 2014 Share Posted September 19, 2014 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
Tails Posted September 19, 2014 Author Share Posted September 19, 2014 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
Saml1er Posted September 19, 2014 Share Posted September 19, 2014 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
Tails Posted September 19, 2014 Author Share Posted September 19, 2014 Nothing changed. The first function works fine without the second one. I just can't figure it out Link to comment
Moderators IIYAMA Posted September 19, 2014 Moderators Share Posted September 19, 2014 Disable respawning while running the timer, keep in control of the methods you are using. Because they are working against each other. Link to comment
Tails Posted September 20, 2014 Author Share Posted September 20, 2014 How can I do that? Link to comment
Moderators IIYAMA Posted September 20, 2014 Moderators Share Posted September 20, 2014 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
Tails Posted September 20, 2014 Author Share Posted September 20, 2014 That seems to work, thanks! What did you change and why does it work now, can you explain? Link to comment
Moderators IIYAMA Posted September 20, 2014 Moderators Share Posted September 20, 2014 after the vehicle did explode I disabled respawning the vehicle. (timer starts) toggleVehicleRespawn(source,false) and when the timer ends I enabled it again. toggleVehicleRespawn(vehicle,true) Link to comment
Tails Posted September 20, 2014 Author Share Posted September 20, 2014 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 IIYAMA Posted September 20, 2014 Moderators Share Posted September 20, 2014 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
Tails Posted September 20, 2014 Author Share Posted September 20, 2014 I chose setVehicleIdleRespawnDelay because I want the vehicle to respawn after someone has left it somewhere. Can't believe this stuff is so tricky... I'll look into it a bit more later. Thanks Link to comment
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now