Why doesn't this debounce work?

hello there! I’ve been going at this for a while and I still can’t get the debounce to work. All help would be appreciated <3

local player = game.Players.LocalPlayer
local Character = player.Character or player.CharacterAdded:Wait()
local ReplicatedS = game:GetService("ReplicatedStorage")
local Freeze = ReplicatedS:WaitForChild("Freeze")
local mouse = player:GetMouse()
local UIS = game:GetService("UserInputService")
local Sound = Instance.new("Sound")
Sound.SoundId = "rbxassetid://"
local Animation = Instance.new("Animation")
Animation.AnimationId = "rbxassetid://"
local debounce = false

player.Chatted:Connect(function(msg)
	if msg == "Immobilus" or "Consto" then
		if Character.Witch.Value == true then
			if Character.Magic.Value > 40 then
				mouse.Button1Down:Connect(function()
					debounce = true
					local Anim = Character:WaitForChild("Humanoid"):LoadAnimation(Animation)
					Sound:Play()
					Anim:Play()
					Freeze:FireServer(mouse.Target)
				end)
				wait(10)
				debounce = false
			end
		end
	end
end)
1 Like

There is no if statement including the debounce variable.

example:

local debouce = false

player.Chatted:Connect(function()
    if debounce then
        debounce = false
   else
        debounce = true
   end
end)

or

local debouce = false

player.Chatted:Connect(function()
    if not debounce then
        debounce = true
   end
  wait(10) -- wait() is depreciated, use task.wait(10) instead
   debounce = false
end)
1 Like

There is no debounce in this script.

You need to do

if debounce == false then
   -- run
else
   -- don't run
end

I updated it to look more like the second example you gave me but still didn’t work. Did I go wrong somewhere?

local player = game.Players.LocalPlayer
local Character = player.Character or player.CharacterAdded:Wait()
local ReplicatedS = game:GetService("ReplicatedStorage")
local Freeze = ReplicatedS:WaitForChild("Freeze")
local mouse = player:GetMouse()
local UIS = game:GetService("UserInputService")
local Sound = Instance.new("Sound")
Sound.SoundId = "rbxassetid://"
local Animation = Instance.new("Animation")
Animation.AnimationId = "rbxassetid://"
local debounce = false

player.Chatted:Connect(function(msg)
	if msg == "Immobilus" or "Consto" then
		if not debounce then
			debounce = true
			if Character.Witch.Value == true then
				if Character.Magic.Value > 40 then
					mouse.Button1Down:Connect(function()
						local Anim = Character:WaitForChild("Humanoid"):LoadAnimation(Animation)
						Sound:Play()
						Anim:Play()
						Freeze:FireServer(mouse.Target)
					end)
				end
			end
		end
	end
	task.wait(10)
	debounce = false
end)
1 Like

Sorry what is this? Use only wait(10) I have no idea why you used task.wait(10)

Simply have the debounce part inside the if statement itself (where you check for it).
This will work out for you (my version), modify if you want.

local player = game.Players.LocalPlayer
local Character = player.Character or player.CharacterAdded:Wait()
local ReplicatedS = game:GetService("ReplicatedStorage")
local Freeze = ReplicatedS:WaitForChild("Freeze")
local mouse = player:GetMouse()
local UIS = game:GetService("UserInputService")
local Sound = Instance.new("Sound")
Sound.SoundId = "rbxassetid://"
local Animation = Instance.new("Animation")
Animation.AnimationId = "rbxassetid://"
local debounce = false

player.Chatted:Connect(function(msg)
	if msg == "Immobilus" or "Consto" then
		if debounce then return end
		debounce = true
		
		if Character.Witch.Value == true then
			if Character.Magic.Value > 40 then
				mouse.Button1Down:Connect(function()
					local Anim = Character:WaitForChild("Humanoid"):LoadAnimation(Animation)
					Sound:Play()
					Anim:Play()
					Freeze:FireServer(mouse.Target)
				end)
			end	
		end
		
		task.wait(10)
		debounce = false
	end
end)
1 Like

That doesn’t change anything his code is practically doing the same thing

Because wait(n) is replaced by task.wait(n), which uses Roblox’s task scheduler.

Wait would do the job, there’s no need for task.wait

That does change something, the debounce part itself.
The task.wait(n) would run when the player chats, not if the debounce is false.

1 Like

Here you go, enjoy your day.

1 Like

Needs to be
if msg == "Immobilus" or msg == "Consto" then

task.wait() is more effective than wait() and wait() is soon to be deprecated.

local player = game.Players.LocalPlayer
local Character = player.Character or player.CharacterAdded:Wait()
local ReplicatedS = game:GetService("ReplicatedStorage")
local Freeze = ReplicatedS:WaitForChild("Freeze")
local mouse = player:GetMouse()
local UIS = game:GetService("UserInputService")
local Sound = Instance.new("Sound")
Sound.SoundId = "rbxassetid://"
local Animation = Instance.new("Animation")
Animation.AnimationId = "rbxassetid://"
local debounce = false

player.Chatted:Connect(function(msg)
	if msg == "Immobilus" or msg == "Consto" then
		if Character.Witch.Value then
			if Character.Magic.Value >= 40 then
				mouse.Button1Down:Connect(function()
					if debounce then return end
					debounce = true
					local Anim = Character:WaitForChild("Humanoid"):LoadAnimation(Animation)
					Sound:Play()
					Anim:Play()
					Freeze:FireServer(mouse.Target)
					task.wait(10)
					debounce = false
				end)
			end
		end
	end
end)
1 Like