So in my game i have a problem with car button mass spawning and lagging the servers a lot.
The problem is that some people click many times and it just spawn a bunch of them and they stack up.
I was thinking of adding a wait() instance, but after i was thinking, yeah this could work, but after thinking, they could stack up and just work like normally and lag and nothing in favour.
-- Local script under the button.
script.Parent.MouseButton1Click:Connect(function(player)
game.ReplicatedStorage.CarSpawning.Carabineros.Dodge:FireServer()
end)
Just add a debounce to prevent multiple cars from spawning at once?
-- Local script under the button.
local DB = false
script.Parent.MouseButton1Click:Connect(function(player)
if DB == false then
DB = true
game.ReplicatedStorage.CarSpawning.Carabineros.Dodge:FireServer()
wait(10)
DB = false
end
end)
You could also implement a sanity check if there’s already a car touching the spawn area using GetTouchingParts() maybe?
i’ll try this out, thanks! also the server script has something that if player spawns a car, the older one get deleted, but players managed to bypass that and mass spawn them really quickly.
Everyone’s methods will work as they incorporate a debounce, but I’ll just mention my version as well
local DB = false
script.Parent.MouseButton1Click:Connect(function()
if DB then return end
DB = true
game.ReplicatedStorage.CarSpawning.Carabineros.Dodge:FireServer()
wait(10)
DB = false
end)
I always believe that if you have an if statement that doesn’t care about the other results, like in this cause, a guard clause should be used to help improve the readability of the code as there will be less indentation.
Also I"m surprised that no one noticed @OP tried to get the player instance from MouseButton1Click, which will not work as it returns the x and y coordinates of where you clicked when the event was activated
while wait(x) do
-- Local script under the button.
script.Parent.MouseButton1Click:Connect(function
(player)
game.ReplicatedStorage.CarSpawning.Carabineros.Dodge:FireServer()
end)
I do not understand what are you trying to do, that’s literally not gonna help at all as it’ll just make another MouseButton1Click event, it wont debounce it at all, the issue will still happen
There’s no need to be using a loop, the Event will fire when the button is first clicked on so your code isn’t necessary to add that little bit more
local DB = false
local TimeToWait = 10
script.Parent.MouseButton1Click:Connect(function(player)
if DB == false then
game.ReplicatedStorage.CarSpawning.Carabineros.Dodge:FireServer()
DB = true
wait(TimeToWait)
DB = false
else
print("You cannot spawn a car right now!")
end
end)