Adding a chat command debounce

I want to know where to put an else statement for the Countdown value. Basically I want it so if the command has said, the player has to wait 60 seconds (1 minute) before they can say it again. I’m just not sure where to put the else statement. You can see the script below.

game.Players.PlayerAdded:Connect(function(Player)
	Player.Chatted:Connect(function(message)
		local Cooldown = false
		-- If the player types "help"
		if string.find(message, "!help") then
			if Cooldown == false then
				local HelpEvent = game.ReplicatedStorage.Events.HelpEvent
				local userId = Player.UserId
				local HelpGui = script.HelpFrame:Clone()
				HelpGui.Name = Player.Name
				HelpGui.Parent = Player.PlayerGui.Waiting
				HelpGui.Visible = false
				local thumbType = Enum.ThumbnailType.HeadShot
				local thumbSize = Enum.ThumbnailSize.Size100x100
				local content, isReady = game.Players:GetUserThumbnailAsync(userId, thumbType, thumbSize)
				HelpGui.PlayerIcon.Image = content
				HelpGui.PlayerName.Text = Player.Name
				
				if Player:GetRankInGroup(12556758) >= 8 then
					local WaitingGui = Player.PlayerGui:WaitForChild("Waiting")
					local ScrollingFrame = Player.PlayerGui.HelpGui.Scroll
					for i, v in pairs(WaitingGui:GetChildren()) do
						v.Parent = ScrollingFrame
						v.Visible = true
					end
				end
			end
		end
	end)
end)

do you want to run the else when they say “!help”? if yes, maybe put it somewhere here?

game.Players.PlayerAdded:Connect(function(Player)
	Player.Chatted:Connect(function(message)
		local Cooldown = false
		-- If the player types "help"
		if string.find(message, "!help") then
			if Cooldown == false then
				local HelpEvent = game.ReplicatedStorage.Events.HelpEvent
				local userId = Player.UserId
				local HelpGui = script.HelpFrame:Clone()
				HelpGui.Name = Player.Name
				HelpGui.Parent = Player.PlayerGui.Waiting
				HelpGui.Visible = false
				local thumbType = Enum.ThumbnailType.HeadShot
				local thumbSize = Enum.ThumbnailSize.Size100x100
				local content, isReady = game.Players:GetUserThumbnailAsync(userId, thumbType, thumbSize)
				HelpGui.PlayerIcon.Image = content
				HelpGui.PlayerName.Text = Player.Name
				
				if Player:GetRankInGroup(12556758) >= 8 then
					local WaitingGui = Player.PlayerGui:WaitForChild("Waiting")
					local ScrollingFrame = Player.PlayerGui.HelpGui.Scroll
					for i, v in pairs(WaitingGui:GetChildren()) do
						v.Parent = ScrollingFrame
						v.Visible = true
					end
				end
            else
                --------------here?
			end
		end
	end)
end)
1 Like

Doesn’t do anything, still runs the same. Here are the changes I made to it.

game.Players.PlayerAdded:Connect(function(Player)
	Player.Chatted:Connect(function(message)
		local Cooldown = false
		-- If the player types "help"
		if string.find(message, "!help") then
			if Cooldown == false then
				local HelpEvent = game.ReplicatedStorage.Events.HelpEvent
				local userId = Player.UserId
				local HelpGui = script.HelpFrame:Clone()
				HelpGui.Name = Player.Name
				HelpGui.Parent = Player.PlayerGui.Waiting
				HelpGui.Visible = false
				local thumbType = Enum.ThumbnailType.HeadShot
				local thumbSize = Enum.ThumbnailSize.Size100x100
				local content, isReady = game.Players:GetUserThumbnailAsync(userId, thumbType, thumbSize)
				HelpGui.PlayerIcon.Image = content
				HelpGui.PlayerName.Text = Player.Name
				HelpGui.Description.Text = Player.Name.." needs help."
				
				if Player:GetRankInGroup(12556758) >= 8 then
					local WaitingGui = Player.PlayerGui:WaitForChild("Waiting")
					local ScrollingFrame = Player.PlayerGui.HelpGui.Scroll
					for i, v in pairs(WaitingGui:GetChildren()) do
						v.Parent = ScrollingFrame
						v.Visible = true
						v.Handler.Disabled = false

						Cooldown = true
					end
				end
			else
				wait(60)
				Cooldown = false
			end
		end
	end)
end)

you’re setting it to false before checking it, making the else useless. may i know why?

1 Like

Because if I made it false at the start and then change the value to true at the end, when they type !help again the else will “start”.

oh, so it’s a debounce system?

1 Like

Correct, its a debounce system.

Can you try if this would work?

game.Players.PlayerAdded:Connect(function(Player)
	Player.Chatted:Connect(function(message)
		local Cooldown = false
		-- If the player types "help"
		if string.find(message, "!help") then
			if Cooldown == false then
                Cooldown = true
				local HelpEvent = game.ReplicatedStorage.Events.HelpEvent
				local userId = Player.UserId
				local HelpGui = script.HelpFrame:Clone()
				HelpGui.Name = Player.Name
				HelpGui.Parent = Player.PlayerGui.Waiting
				HelpGui.Visible = false
				local thumbType = Enum.ThumbnailType.HeadShot
				local thumbSize = Enum.ThumbnailSize.Size100x100
				local content, isReady = game.Players:GetUserThumbnailAsync(userId, thumbType, thumbSize)
				HelpGui.PlayerIcon.Image = content
				HelpGui.PlayerName.Text = Player.Name
				
				if Player:GetRankInGroup(12556758) >= 8 then
					local WaitingGui = Player.PlayerGui:WaitForChild("Waiting")
					local ScrollingFrame = Player.PlayerGui.HelpGui.Scroll
					for i, v in pairs(WaitingGui:GetChildren()) do
						v.Parent = ScrollingFrame
						v.Visible = true
					end
                        wait(60)
                        Cooldown = false
                else
                    wait(60)
                    Cooldown = false
				end
			end
		end
	end)
end)

That will not work because every time the player chats the cooldown is set to false so the debounce doesn’t work

oh yes, right. maybe something like this?

local Cooldown = false

game.Players.PlayerAdded:Connect(function(Player)
	Player.Chatted:Connect(function(message)
		-- If the player types "help"
		if string.find(message, "!help") then
			if Cooldown == false then
                Cooldown = true
				local HelpEvent = game.ReplicatedStorage.Events.HelpEvent
				local userId = Player.UserId
				local HelpGui = script.HelpFrame:Clone()
				HelpGui.Name = Player.Name
				HelpGui.Parent = Player.PlayerGui.Waiting
				HelpGui.Visible = false
				local thumbType = Enum.ThumbnailType.HeadShot
				local thumbSize = Enum.ThumbnailSize.Size100x100
				local content, isReady = game.Players:GetUserThumbnailAsync(userId, thumbType, thumbSize)
				HelpGui.PlayerIcon.Image = content
				HelpGui.PlayerName.Text = Player.Name
				
				if Player:GetRankInGroup(12556758) >= 8 then
					local WaitingGui = Player.PlayerGui:WaitForChild("Waiting")
					local ScrollingFrame = Player.PlayerGui.HelpGui.Scroll
					for i, v in pairs(WaitingGui:GetChildren()) do
						v.Parent = ScrollingFrame
						v.Visible = true
					end
                        wait(60)
                        Cooldown = false
                else
                    wait(60)
                    Cooldown = false
				end
			end
		end
	end)
end)

Yes, but there’s no need to put another wait cooldown on the 2nd else

Something like this

local Cooldown = false
local CooldownTime = 60

game.Players.PlayerAdded:Connect(function(Player)
	Player.Chatted:Connect(function(message)
		if string.find(message, "!help") then
			if Cooldown == false then
				Cooldown = true
			
				local HelpEvent = game.ReplicatedStorage.Events.HelpEvent
				local userId = Player.UserId
				local HelpGui = script.HelpFrame:Clone()
				HelpGui.Name = Player.Name
				HelpGui.Parent = Player.PlayerGui.Waiting
				HelpGui.Visible = false
				local thumbType = Enum.ThumbnailType.HeadShot
				local thumbSize = Enum.ThumbnailSize.Size100x100
				local content, isReady = game.Players:GetUserThumbnailAsync(userId, thumbType, thumbSize)
				HelpGui.PlayerIcon.Image = content
				HelpGui.PlayerName.Text = Player.Name

				if Player:GetRankInGroup(12556758) >= 8 then
					local WaitingGui = Player.PlayerGui:WaitForChild("Waiting")
					local ScrollingFrame = Player.PlayerGui.HelpGui.Scroll
					for i, v in pairs(WaitingGui:GetChildren()) do
						v.Parent = ScrollingFrame
						v.Visible = true
					end
				end
				
				wait(CooldownTime)
				Cooldown = false
			else
				print("!help is not available yet because you are on cooldown")
			end
		end
	end)
end)
1 Like