igoor.66_ Posted January 11 Share Posted January 11 I have a script where I get a car by command (/cm [ID] ) and this script does not have a spawn limit and I would like to set a limit function makeVehicleCmd(thePlayer, cmd, id) id = tonumber(id) if not id then return outputChatBox("SYNTAX: /"..cmd.." [Vehicle ID]", thePlayer, 255,194,14) end local x,y,z = getElementPosition(thePlayer) local rx,ry,rz = getElementRotation(thePlayer) local element = createTestElement(thePlayer, "vehicle", id, x,y,z, rx,ry,rz) if element then setElementPosition(thePlayer, x,y,z+3) setElementInterior(element, getElementInterior(thePlayer)) setElementDimension(element, getElementDimension(thePlayer)) end end addCommandHandler("cm", makeVehicleCmd, false, false) Link to comment
mazarati21 Posted January 11 Share Posted January 11 Hi there try this: local playerVehicleCount = {} -- Table to keep track of player vehicle counts local maxVehiclesPerPlayer = 5 -- Maximum number of vehicles a player can spawn function makeVehicleCmd(thePlayer, cmd, id) id = tonumber(id) if not id then return outputChatBox("SYNTAX: /"..cmd.." [Vehicle ID]", thePlayer, 255, 194, 14) end -- Initialize player's vehicle count if not already done if not playerVehicleCount[thePlayer] then playerVehicleCount[thePlayer] = 0 end -- Check if player has reached the spawn limit if playerVehicleCount[thePlayer] >= maxVehiclesPerPlayer then return outputChatBox("You have reached the maximum number of vehicles you can spawn.", thePlayer, 255, 0, 0) end local x, y, z = getElementPosition(thePlayer) local rx, ry, rz = getElementRotation(thePlayer) local element = createTestElement(thePlayer, "vehicle", id, x, y, z, rx, ry, rz) if element then setElementPosition(thePlayer, x, y, z + 3) setElementInterior(element, getElementInterior(thePlayer)) setElementDimension(element, getElementDimension(thePlayer)) playerVehicleCount[thePlayer] = playerVehicleCount[thePlayer] + 1 -- Increment the vehicle count end end addCommandHandler("cm", makeVehicleCmd, false, false) -- Optional: Reset vehicle count when a player leaves the server function resetPlayerVehicleCount() playerVehicleCount[source] = nil end addEventHandler("onPlayerQuit", getRootElement(), resetPlayerVehicleCount) Link to comment
igoor.66_ Posted January 11 Author Share Posted January 11 (edited) Edited January 11 by igoor.66_ Link to comment
igoor.66_ Posted January 11 Author Share Posted January 11 19 minutes ago, igoor.66_ said: 2 hours ago, mazarati21 said: Hi there try this: local playerVehicleCount = {} -- Table to keep track of player vehicle counts local maxVehiclesPerPlayer = 5 -- Maximum number of vehicles a player can spawn function makeVehicleCmd(thePlayer, cmd, id) id = tonumber(id) if not id then return outputChatBox("SYNTAX: /"..cmd.." [Vehicle ID]", thePlayer, 255, 194, 14) end -- Initialize player's vehicle count if not already done if not playerVehicleCount[thePlayer] then playerVehicleCount[thePlayer] = 0 end -- Check if player has reached the spawn limit if playerVehicleCount[thePlayer] >= maxVehiclesPerPlayer then return outputChatBox("You have reached the maximum number of vehicles you can spawn.", thePlayer, 255, 0, 0) end local x, y, z = getElementPosition(thePlayer) local rx, ry, rz = getElementRotation(thePlayer) local element = createTestElement(thePlayer, "vehicle", id, x, y, z, rx, ry, rz) if element then setElementPosition(thePlayer, x, y, z + 3) setElementInterior(element, getElementInterior(thePlayer)) setElementDimension(element, getElementDimension(thePlayer)) playerVehicleCount[thePlayer] = playerVehicleCount[thePlayer] + 1 -- Increment the vehicle count end end addCommandHandler("cm", makeVehicleCmd, false, false) -- Optional: Reset vehicle count when a player leaves the server function resetPlayerVehicleCount() playerVehicleCount[source] = nil end addEventHandler("onPlayerQuit", getRootElement(), resetPlayerVehicleCount) Hello, so the code worked perfectly, but now I can't destroy the vehicle to get another one, even destroying it through the dashboard, the script recognizes that I still have it and won't let me get any more, just by relogging, do you know how to fix this? Link to comment
mazarati21 Posted January 15 Share Posted January 15 (edited) Try this way local playerVehicleCount = {} -- Table to keep track of player vehicle counts local maxVehiclesPerPlayer = 5 -- Maximum number of vehicles a player can spawn function makeVehicleCmd(thePlayer, cmd, id) id = tonumber(id) if not id then return outputChatBox("SYNTAX: /"..cmd.." [Vehicle ID]", thePlayer, 255, 194, 14) end if not playerVehicleCount[thePlayer] then playerVehicleCount[thePlayer] = 0 end if playerVehicleCount[thePlayer] >= maxVehiclesPerPlayer then return outputChatBox("You have reached the maximum number of vehicles you can spawn.", thePlayer, 255, 0, 0) end local x, y, z = getElementPosition(thePlayer) local rx, ry, rz = getElementRotation(thePlayer) local vehicle = createTestElement(thePlayer, "vehicle", id, x, y, z, rx, ry, rz) if vehicle then setElementPosition(thePlayer, x, y, z + 3) setElementInterior(vehicle, getElementInterior(thePlayer)) setElementDimension(vehicle, getElementDimension(thePlayer)) playerVehicleCount[thePlayer] = playerVehicleCount[thePlayer] + 1 -- Attach an event handler to decrease vehicle count when the vehicle is destroyed addEventHandler("onElementDestroy", vehicle, function() if playerVehicleCount[thePlayer] then playerVehicleCount[thePlayer] = math.max(0, playerVehicleCount[thePlayer] - 1) end end) end end addCommandHandler("cm", makeVehicleCmd, false, false) function resetPlayerVehicleCount() playerVehicleCount[source] = nil end addEventHandler("onPlayerQuit", getRootElement(), resetPlayerVehicleCount) Edited January 15 by mazarati21 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