Tails Posted September 19, 2014 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 Discord: its.tails
Callum Posted September 19, 2014 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. Retired
Tails Posted September 19, 2014 Author 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? Discord: its.tails
Saml1er Posted September 19, 2014 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
Tails Posted September 19, 2014 Author Posted September 19, 2014 Nothing changed. The first function works fine without the second one. I just can't figure it out Discord: its.tails
Moderators IIYAMA Posted September 19, 2014 Moderators 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. Do you want to improve your Lua programming skills and make less mistakes? Start with Lua Language Server! Useful functions 3x Spoiler checkPassiveTimer getScreenStartPositionFromBox getPedGender Tutorials 4x Spoiler Scaling DX Events Attach an addEventHandler on a group of elements Debugging
Tails Posted September 20, 2014 Author Posted September 20, 2014 How can I do that? Discord: its.tails
Moderators IIYAMA Posted September 20, 2014 Moderators 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) Do you want to improve your Lua programming skills and make less mistakes? Start with Lua Language Server! Useful functions 3x Spoiler checkPassiveTimer getScreenStartPositionFromBox getPedGender Tutorials 4x Spoiler Scaling DX Events Attach an addEventHandler on a group of elements Debugging
Tails Posted September 20, 2014 Author Posted September 20, 2014 That seems to work, thanks! What did you change and why does it work now, can you explain? Discord: its.tails
Moderators IIYAMA Posted September 20, 2014 Moderators 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) Do you want to improve your Lua programming skills and make less mistakes? Start with Lua Language Server! Useful functions 3x Spoiler checkPassiveTimer getScreenStartPositionFromBox getPedGender Tutorials 4x Spoiler Scaling DX Events Attach an addEventHandler on a group of elements Debugging
Tails Posted September 20, 2014 Author 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? Discord: its.tails
Moderators IIYAMA Posted September 20, 2014 Moderators 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. Do you want to improve your Lua programming skills and make less mistakes? Start with Lua Language Server! Useful functions 3x Spoiler checkPassiveTimer getScreenStartPositionFromBox getPedGender Tutorials 4x Spoiler Scaling DX Events Attach an addEventHandler on a group of elements Debugging
Tails Posted September 20, 2014 Author 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 Discord: its.tails
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