GabWas Posted January 28, 2016 Share Posted January 28, 2016 Hi there, I have a problem with my script for temporary cars. After leaving them, they should be gone and it's working fine, but when someone else create other temporary car, previous one doesn't disappear. I think that I must use tables, but I don't understand them. The tutorials aren't helping me. Here is the code: function cmd_veh(plr, cmd, ...) local vehicleName = table.concat({...}, " ") local vehicleID = getVehicleModelFromName(vehicleName) local x, y, z = getElementPosition(plr) if isPedInVehicle(plr) then outputChatBox ("#C80000✖ #E7D9B0Get out of the vehicle first.", plr, 255, 255, 255, true) return end if vehicleID then newVehicle = createVehicle (vehicleID, x, y, z, 0, 0, 0) warpPedIntoVehicle(plr, newVehicle) outputChatBox ("#04B404✔ #E7D9B0You created a temporary vehicle named " .. vehicleName .. "#E7D9B0.", plr, 255, 255, 255, true) end end addCommandHandler("veh", cmd_veh) function deleteTempVeh(plr, seat, jacked) if not (newVehicle) then return end if (newVehicle) then destroyElement(newVehicle) end end addEventHandler("onVehicleExit", getRootElement(), deleteTempVeh) Link to comment
NewbProgramming Posted January 28, 2016 Share Posted January 28, 2016 Yes you need to use a table and use the player element for the index. -- global table player_vehicle = {}; -- in the cmd_veh function if player_vehicle[plr] ~= false then destroyElement(player_vehicle[plr]); end player_vehicle[plr] = createVehicle(...) -- in onPlayerQuit event handler if player_vehicle[plr] ~= false then destroyElement(player_vehicle[plr]); end Link to comment
GabWas Posted January 28, 2016 Author Share Posted January 28, 2016 Maybe I'm doing it wrong, but it's not working. We tested that out with friend; he created car first, I did it after him, and on leaving - his car disappear, not mine. Link to comment
NewbProgramming Posted January 28, 2016 Share Posted January 28, 2016 Did you destroyElement(player_vehicle[plr])? Have to completely remove the newVehicle variable and replace it with the table variable. Link to comment
NewbProgramming Posted January 28, 2016 Share Posted January 28, 2016 Try: TEMPORARY_VEHICLE = {}; function cmd_veh(plr, cmd, ...) local vehicleName = table.concat({...}, " ") local vehicleID = getVehicleModelFromName(vehicleName) local x, y, z = getElementPosition(plr) if isPedInVehicle(plr) then outputChatBox ("#C80000✖ #E7D9B0Get out of the vehicle first.", plr, 255, 255, 255, true) return end if vehicleID then if TEMPORARY_VEHICLE[plr] ~= false then destroyElement(TEMPORARY_VEHICLE[plr]); end TEMPORARY_VEHICLE[plr] = createVehicle (vehicleID, x, y, z, 0, 0, 0) warpPedIntoVehicle(plr, TEMPORARY_VEHICLE[plr]) outputChatBox ("#04B404✔ #E7D9B0You created a temporary vehicle named " .. vehicleName .. "#E7D9B0.", plr, 255, 255, 255, true) end end addCommandHandler("veh", cmd_veh) function deleteTempVeh(plr, seat, jacked) if not TEMPORARY_VEHICLE[plr] then return end destroyElement(TEMPORARY_VEHICLE[plr]) end addEventHandler("onVehicleExit", getRootElement(), deleteTempVeh) function PlayerQuit() if not TEMPORARY_VEHICLE[source] then return end destroyElement(TEMPORARY_VEHICLE[source]) end addEventHandler"onPlayerQuit", getRootElement(), PlayerQuit); Link to comment
GabWas Posted January 28, 2016 Author Share Posted January 28, 2016 Thank you, it's working like a charm! But one more problem is, that you can create only one car at the time. Is there any way to create multiple cars? For example there is an event and administrator is creating cars for players. Is there any way to do it like that? Because I saw that on some servers. Link to comment
NewbProgramming Posted January 28, 2016 Share Posted January 28, 2016 I am going to sleep, if you still need help when I wake up I'll be happy to quickly script what you want. Private Message me. Link to comment
GabWas Posted January 28, 2016 Author Share Posted January 28, 2016 Okay then. Anyway, thank you for your help 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