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

Scripting debounce/not allowing people to spam?

So, right now I have a script that's functioning really well except for one problem, which is the player can spam the letter Z and it starts to lag the game. I understand it's debounce, but when in the script should I place it, or is there a replacement for debounce that i can put, like wait()? The variable "Ready" I have in my script is the current debounce I have, but it still allows the player to spam Z.

player = game.Players.LocalPlayer 
character = player.CharacterAdded:wait()
humanoid = character:WaitForChild('Humanoid') 
mouse = player:GetMouse() 

goroBall = Instance.new("Part", game.Workspace)
goroBall.Shape = "Ball" 
goroBall.CanCollide = false
goroBall.Material = "Neon" 
goroBall.BrickColor = BrickColor.new("Bright yellow") 
goroBall.Size = Vector3.new(15,15,15) 

uis = game:GetService("UserInputService")

uis.InputBegan:connect(function(input,gpe)
	local Ready = true
	if input.KeyCode == Enum.KeyCode.Z and Ready == true then
		Ready = false 
		for i = 1,5 do
			local goro = goroBall:Clone()
			goro.Parent = game.Workspace
			goro.CFrame = character.Torso.CFrame * CFrame.new(0,0,-15)
			local bv = Instance.new("BodyVelocity")
			bv.MaxForce = Vector3.new(1e8,1e8,1e8)
			bv.Velocity = character.Torso.CFrame.lookVector * 50
			bv.Parent = goro
			Ready = true
		end
    end  
end)


1 answer

✓ Accepted answer

This is because your setting Ready to true right away.This will allow players to spam. Suggest you do this format

local var1 = blah --I got bored, just replace these with your variables
local var2 = blah
local var3 = blah
local ready = false --U have to create the var outside

goroBall = Instance.new("Part", game.Workspace)
goroBall.Shape = "Ball"
goroBall.CanCollide = false
goroBall.Material = "Neon"
goroBall.BrickColor = BrickColor.new("Bright yellow")
goroBall.Size = Vector3.new(15,15,15)
 
uis = game:GetService("UserInputService")

is.InputBegan:connect(function(input,gpe)
    if input.KeyCode == Enum.KeyCode.Z and ready == false then
         for i = 1,5 do
            local goro = goroBall:Clone()
            goro.Parent = game.Workspace
            goro.CFrame = character.Torso.CFrame * CFrame.new(0,0,-15)
            local bv = Instance.new("BodyVelocity")
            bv.MaxForce = Vector3.new(1e8,1e8,1e8)
            bv.Velocity = character.Torso.CFrame.lookVector * 50
            bv.Parent = goro
            ready = true -- changes ready to false so you can't activate anymore
		wait(5) --Change this to how long you wanna wait untill useable again
		Ready = false --U can use again
        end
    end 
end)

If this helped please accept answer

6 comments

0

err sorry, but im still able to spam "z" and it wont work

0

Ehh sorry, I edited by answer, I made a mistake

0

You should move the ready = true outside, above, the for loop.

0

wait lol im confused, if "Ready" is false, at the beginning, script won't run because it's false right? or is it meant to be like that...?

0

sorry it worked nvm XD

0

ok sorry one last question, i wanted the script to send out multiple spheres at one time, but instead it only sends out one sphere every few seconds. Is this the result of using a for loop, and if so which loop do i use? while true do?

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