Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

I am wondering on how to add a wait before spawning another object from a GUI button?

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) 

1 answer

All you have to do is add debounce.

local debounce = false
script.Parent.MouseButton1Click:Connect(
    function()
        if debounce == false then
            debounce = true
            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()
                                else
                                    wait(20)
                                    debounce = false
                                end
                            end
                        end
                    )
                end
            end
        end
    end
)

2 comments

0

I can purchase the car but it wont spawn!

0

If the spawnings the issue than the problem is with your script.

This discussion is preserved as it was. New answers, comments, and votes are closed.