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

How do I add a delay to this so i cant spam click it?

local tool = script.Parent

local damage = 5

local inner = tool.Emitter.Inner	
local outer = tool.Emitter.Inner

tool:WaitForChild("Blade").Touched:Connect(function (hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		hit.Parent.Humanoid:TakeDamage(damage)
	end
end)

tool:WaitForChild("RemoteEvent").OnServerEvent:Connect(function (player, state)
	if state then
		tool.Emitter.Inner.Enabled = true
		tool.Emitter.Outer.Enabled = true
	else
		inner.Parent = nil
		outer.Parent = nil
		
		inner.Enabled = false
		outer.Enabled = false
		
		wait(2)
		
		inner.Parent = tool.Emitter
		outer.Parent = tool.Emitter
		
	end
end)

1 comment

1

what do you want to add the delay onto? like what line?

3 answers

Idk where you want this delay but I tried this.

local tool = script.Parent

local damage = 5

local inner = tool.Emitter.Inner    
local outer = tool.Emitter.Inner

local debouce = false

tool:WaitForChild("Blade").Touched:Connect(function (hit)
    if hit.Parent:FindFirstChild("Humanoid") then
        hit.Parent.Humanoid:TakeDamage(damage)
    end
end)

tool:WaitForChild("RemoteEvent").OnServerEvent:Connect(function (player, state)
	if debouce == false then
		debouce = true
	    if state then
	        tool.Emitter.Inner.Enabled = true
	        tool.Emitter.Outer.Enabled = true
	    else
	        inner.Parent = nil
	        outer.Parent = nil
	
	        inner.Enabled = false
	        outer.Enabled = false
	
	        wait(2)
	
	        inner.Parent = tool.Emitter
	        outer.Parent = tool.Emitter
		end
		wait(1) -- change your delay here
		debouce = false
	elseif debouce == true then
		print("Don't spam click it :)")
    end
end)

because you didn't know where you wanted to put this i guessed that you want it in the .touched so you would replace the .touched with:

delay = false
tool:WaitForChild("Blade").Touched:Connect(function (hit)
    if hit.Parent:FindFirstChild("Humanoid") then
	if not delay then
		delay = true
        	hit.Parent.Humanoid:TakeDamage(damage)
		wait(2)
		delay = false
	end
    end
end)

3 comments

0

on line 5 where the 2 is put how long you want the delay to be for in seconds

0

That is not the correct format, that event can only be used once..

0

how to delete another persons comment

Hey there! Your most reliable option is tick() which get's the amount of seconds from 1970 Jan 1, we can compare this value to 0 to make a cooldown!

local tool = script.Parent

local damage = 5
local Cooldown = 0
local Cooldowns = {} --// To use for touched event!
local inner = tool.Emitter.Inner
local outer = tool.Emitter.Inner

tool:WaitForChild("Blade").Touched:Connect(function (hit) --// Let's try something different with taking damage!
    --// we need to save tick to humanoid of each humanoid so there won't be a mix up
    if hit.Parent:FindFirstChild("Humanoid") then
        local Humanoid = hit.Parent.Humanoid
        if not Cooldowns[Humanoid] or tick() - Cooldowns[Humanoid] > 1 then
            Cooldowns[Humanoid] = tick()
            hit.Parent.Humanoid:TakeDamage(damage)
        end
end
end)

tool:WaitForChild("RemoteEvent").OnServerEvent:Connect(function (player, state)
    if tick() - Cooldown < 3 then return end --// If it has been less than 3 secs since the last usage, it returns
    Cooldown = tick() --// Set back to tick to be used again!
    if state then
        tool.Emitter.Inner.Enabled = true
        tool.Emitter.Outer.Enabled = true
    else
        inner.Parent = nil
        outer.Parent = nil

        inner.Enabled = false
        outer.Enabled = false

        wait(2)

        inner.Parent = tool.Emitter
        outer.Parent = tool.Emitter

    end
end)

14 comments

0

Why did I put humanoid in Cooldowns? Well, that is because each char has 1 humanoid and you don't want to damage them 15 times or 6 times.

0

Sorry I meant humanoid of each character not Humanoid of each Humanoid

0

Loading...

0

Loading...

0

Loading...

0

Loading...

0

Loading...

0

Loading...

0

Loading...

0

Loading...

0

Loading...

0

Loading...

0

The lightsaber wont swing..

0

Number doge, I forgot, you have to remove all the debounce

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