So I have this code in a car game so it would spawn me cars, but people keep abusing it and spam the spawn button making the entire server lag! I tried adding the delete, but the spamming will still bypass it! Please help me create spawn one car per 20 seconds! Heres the code.
script.Parent.MouseButton1Click:Connect(function()
if script.Parent.Parent.Purchased.Value == true then
-- Spawn it
game.ReplicatedStorage.SpawnCar:FireServer(script.Parent.Parent.Name)
else
-- Let them purchase it
local PriceOfItem = game.ReplicatedStorage:WaitForChild("CheckPrice"):InvokeServer(script.Parent.Parent.Name)
if game.Players.LocalPlayer.leaderstats.Cash.Value >= PriceOfItem and script.Parent.Parent.Purchased.Value == false then
-- The player has enough cash
game.Players.LocalPlayer.leaderstats.Cash.Value = game.Players.LocalPlayer.leaderstats.Cash.Value - PriceOfItem
script.Parent.Parent.Purchased.Value = true
script.Parent.Text = "Spawn"
script.Parent.MouseButton1Click:connect(function()
-- Deletes the car.
for i,v in pairs(game.Workspace:GetChildren()) do
if v.Name == game.Players.LocalPlayer.Name.."'s Tesla Roadster" then
v:Destroy()
end
end
end)
end
end
end)

2 comments
I can purchase the car but it wont spawn!
If the spawnings the issue than the problem is with your script.