How to add cooldown to for, pair loops?

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    I want to make a cloaking device that makes people invisible with a cooldown

  2. What is the issue? Include screenshots / videos if possible!
    I added a script that uses for pair loops to make the player invisible upon pressing mouse2button. But even though I already added debounce, the loop still starts when the debounce is false

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I tried adding debounce in both the server and local script

After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!
Local Script

local UserInputService = game:GetService("UserInputService")
local TweenService = game:GetService("TweenService")
local Char = game.Players.LocalPlayer.Character
local CanCloak = true

UserInputService.InputBegan:Connect(function(input)
	if input.UserInputType == Enum.UserInputType.MouseButton2 then
		if CanCloak == true then
			CanCloak = false
			script.Parent.CloakEvent:FireServer()
			for _, descendants in pairs(Char:GetDescendants()) do
				if descendants:IsA("BasePart") and descendants.Name ~= "HumanoidRootPart" and not CanCloak then
					local TweenA = TweenService:Create(descendants, TweenInfo.new(1), {Transparency = 0.75})
					Char.Head.face.Transparency = 0.75

					script.Parent.Cloak:Play()
					TweenA:Play()
				end
			end
		else
			script.Parent.DeCloakEvent:FireServer()
			wait(1)
			CanCloak =true
		end
	end
end)

Server Script

script.Parent.Parent.Equipped:Connect(function()
	script.Parent.CloakEvent.OnServerEvent:Connect(function(player)
		local Char = player.Character
		
		for _, descendants in pairs(Char:GetDescendants()) do
			if descendants:IsA("BasePart") and descendants.Name ~= "HumanoidRootPart"  then
				local TweenA = TweenService:Create(descendants, TweenInfo.new(1), {Transparency = 1})
				Char.Head.face.Transparency = 1

				script.Parent.Cloak:Play()
				TweenA:Play()
			end
		end
	end)
end)

script.Parent.Parent.Equipped:Connect(function()
	script.Parent.DeCloakEvent.OnServerEvent:Connect(function(player) 
		local Char = player.Character

		for _, descendants in pairs(Char:GetDescendants()) do
			if descendants:IsA("BasePart") and descendants.Name ~= "HumanoidRootPart" then
				local TweenA = TweenService:Create(descendants, TweenInfo.new(1), {Transparency = 0})
				Char.Head.face.Transparency = 0

				script.Parent.Decloak:Play()
				TweenA:Play()
			end
		end
	end)
end)

Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.

1 Like

sorry, roblox decided that it was time to post my post while I was typing.
Anyway,

first of all, why are you using userInputService & connecting th events on equipped and not disconnecting them on unequipped?
this will cause leaks eventually which will lag your game.

Now, it seems like you only want the event to run when your tool is equipped right, otherwise the handlers wouldn’t be in equipped.

so why can’t you just define the handlers in a separate server scriptservice script.
and then add this localscript to your tool:

local tool = script.Parent
local cloak = --cloak path

local cancloak = false

--only runs when the tool is equipped and pressed with mousclick (so mousebutton1)
tool.Activated:Connect(function()
   if cancloack then
      cancloak = false
       cloak:FireServer(1)
    else
      cancloak = true
      cloak:FirServer(0)
end)

also why are you playing a sound for every descendant and setting char.head.face for every descendant?

you should probably change your server handlers to this:

local cloak = --your cloack event
local cloaksfx = --your cloaksound
local decloaksfx = --your decloak sound
cloak.OnServerEvent:Connect(function(player, transparency)
    local char = player.Character
    if not char then return end

    char.Head.face.Transparency = transparency
    (transparency == 1 and cloaksfx or decloaksfx):Play()

    --ipairs is better, pairs is for dictionaries, ipairs for arrays.
    for _, descendant in ipairs(Char:GetDescendants()) do
        if not descendant:IsA("BasePart") or descendant == char.PrimaryPart then continue end
        local tween = TweenService:Create(descendant, TweenInfo.new(1), {Transparency = transparency })

        tween:Play()
    end
end)

there, now it seems like you want mousebutton2 to be the trigger instead.
so if you after this example still want mousebutton2 to be the trigger you can keep the server side but do this on the client:

local contextActions = game:GetService("ContextActionService")
local tool = --path to your tool
local cancloak = false
local cloak = --path to your remote

tool.Equipped:Connect(function()
  contextActions:BindAction("cloak", function(_, state)
      if state ~= Enum.UserInputState.Begin then return Enum.ContextActionResult.Pass end
      cloak:FireServer(cancloak and 1 or 0)
      cancloak = not cancloak
  end, false, Enum.UserInputType.MouseButton2)
end)

tool.Unequipped:Connect(function()
  contextActions:UnbindAction("cloak")
end)

tool.Destroying:Connect(function()
   contextActions:UnbindAction("cloak")
end)

now regarding your issue, it is because cancloak is not used as a debounce & instead is used as some sort of state saver to check if the player is invisible or not, and then to fire the corresponding event.

to fix this, add an actual debounce:

local UserInputService = game:GetService("UserInputService")
local TweenService = game:GetService("TweenService")
local Char = game.Players.LocalPlayer.Character
local CanCloak = true 

UserInputService.InputBegan:Connect(function(input)
	if input.UserInputType ~= Enum.UserInputType.MouseButton2 then return end
    if not CanCloak then return end--this is how you use debounce
    CanCloak = false
    script.Parent.CloakEvent:FireServer()
	   
    task.wait(1)
    script.Parent.DeCloakEvent:FireServer()
    CanCloak = true
end)

and voilà! Now your script should act like an actual debounce.
the for loop isn’t needed on the client side, because changing it on the server replicates the changes anyway.

Hope this helps!

1 Like

sorry, but the server handler has a error, for the 9th line. I’m a very intermediate programmer and I don’t understand

which handler, mine or yours? I gave you like 3 versions.

1 Like

the third one, the output bar told me to use ‘;’, but since I’m new to this sort of coding, I don’t know how to fix it

1 Like

can you post the output here? (idiotcharlimitthatdidn’tthinkaboutshortanswers)

1 Like
Players.Blazebomb20.PlayerGui.SelectionMenuGui.Frame.SelectionButtons.MeleeSelection.ScreenGui.ScrollingFrame.ButterKnife.Butter Knife and Cloak.Handle.Effect:84: Ambiguous syntax: this looks like an argument list for a function call, but could also be a start of new statement; use ';' to separate statements

okay, what script? I’ve checked all scripts and I don’t see anything that can cause ambiguity, except for this typo in the first script (cloack instead of cloak), . If you don’t see it either try just spamming semicolons after every statement.

sorry, its just that I don’t know how to properly implement this, because I keep getting errors, specifically in your second script for the server on line 8.

i redid my scripts (I use local function, so I don’t get confused), using your debounce, and i also added IsCloaked. It seems to be working, and I don’t seem to see any problems.

local script:

local UserInputService = game:GetService("UserInputService")
local TweenService = game:GetService("TweenService")
local Player = game.Players.LocalPlayer
local Char = Player.Character or Player.CharacterAdded:Wait()
local CanCloak = true
local CooldownTime = 5 -- Set your cooldown time here

local function cloakCharacter()
	wait(0.2)
	for _, descendant in pairs(Char:GetDescendants()) do
		if descendant:IsA("BasePart") and descendant.Name ~= "HumanoidRootPart" then
			local TweenA = TweenService:Create(descendant, TweenInfo.new(1), {Transparency = 0.75})
			TweenA:Play()
		end
	end
	if Char:FindFirstChild("Head") and Char.Head:FindFirstChild("face") then
		Char.Head.face.Transparency = 0.75
	end
end

local function decloakCharacter()
	for _, descendant in pairs(Char:GetDescendants()) do
		if descendant:IsA("BasePart") and descendant.Name ~= "HumanoidRootPart" then
			local TweenA = TweenService:Create(descendant, TweenInfo.new(1), {Transparency = 0})
			TweenA:Play()
		end
	end
	if Char:FindFirstChild("Head") and Char.Head:FindFirstChild("face") then
		Char.Head.face.Transparency = 0
	end
end

local Iscloaked = false

script.Parent.Parent.Equipped:Connect(function()
	UserInputService.InputBegan:Connect(function(input)
		if input.UserInputType == Enum.UserInputType.MouseButton2 then
			if CanCloak then
				if not CanCloak then return end
				CanCloak = false
				Iscloaked = true
				script.Parent.Cloak:Play()
				cloakCharacter()
				script.Parent.CloakEvent:FireServer()
				wait(2)
				if not CanCloak and not Iscloaked then
					CanCloak = true
				end
			else
				if Iscloaked then
					Iscloaked = false
					script.Parent.DeCloakEvent:FireServer()
					decloakCharacter()
					script.Parent.Decloak:Play()
				end
			end
		end
	end)
end)

server script:

local TweenService = game:GetService("TweenService")

local function handleCloak(player)
	local Char = player.Character
	if Char then
		for _, descendant in pairs(Char:GetDescendants()) do
			if descendant:IsA("BasePart") and descendant.Name ~= "HumanoidRootPart" then
				local TweenA = TweenService:Create(descendant, TweenInfo.new(1), {Transparency = 1})
				TweenA:Play()
			end
		end
		if Char:FindFirstChild("Head") and Char.Head:FindFirstChild("face") then
			Char.Head.face.Transparency = 1
		end
	end
end

local function handleDecloak(player)
	local Char = player.Character
	if Char then
		for _, descendant in pairs(Char:GetDescendants()) do
			if descendant:IsA("BasePart") and descendant.Name ~= "HumanoidRootPart" then
				local TweenA = TweenService:Create(descendant, TweenInfo.new(1), {Transparency = 0})
				TweenA:Play()
			end
		end
		if Char:FindFirstChild("Head") and Char.Head:FindFirstChild("face") then
			Char.Head.face.Transparency = 0
		end
	end
end

script.Parent.CloakEvent.OnServerEvent:Connect(handleCloak)
script.Parent.DeCloakEvent.OnServerEvent:Connect(handleDecloak)