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

Insert a Delay on a Function?

Hi, i have a little problem:

(sorry for bad english). I need to insert a delay in this function for 0.5 Seconds. You can activate this function only 1 time for every 0.5 seconds. It's simple, but i don't know how i can do it >.<.

tool.Activated:connect(function()
	local choose = math.random(1,2)
	canattack.Value = true
	if choose == 1 then
		local swing1animation = script.Parent.Parent.Humanoid:LoadAnimation(script.Punch1)
		swing1animation:Play()
		wait(.2)
		swingsound:Play()
	elseif choose == 2 then
		local swing2animation = script.Parent.Parent.Humanoid:LoadAnimation(script.Punch2)
		swing2animation:Play()
		wait(.2)
		swingsound:Play()
	end
end)

Thanks!

1 comment

2 answers

✓ Accepted answer

Change your script to look like this (look under it for the explanation):

local canSwing = true
local DEBOUNCE_TIME = 0.5
tool.Activated:connect(function()
	if canSwing then
		canSwing = false
		local choose = math.random(1,2)
		canattack.Value = true
		if choose == 1 then
			local swing1animation = script.Parent.Parent.Humanoid:LoadAnimation(script.Punch1)
	        	swing1animation:Play()
	        	wait(.2)
	        	swingsound:Play()
	  	elseif choose == 2 then
	        	local swing2animation = script.Parent.Parent.Humanoid:LoadAnimation(script.Punch2)
	        	swing2animation:Play()
	        	wait(.2)
	        	swingsound:Play()
		end
		spawn(function()
			wait(DEBOUNCE_TIME)
			canSwing = true
		end)
	end
end)

We use spawn() to create a new thread (basically as if you are running it on another script, but can access the variables in the script) and run our wait function in there. We then see if canSwing is true (we are swinging for the first time or we have waited DEBOUNCE_TIME amount of time) and, if it is, run the function you made.

Put a Boolean Value inside the Main Script called 'delay', make sure it's false. The boolean value will help the second script able to turn the cooldown into a variable that can be accessed by the main script.

-- Main Script
if script.delay.Value == false then
tool.Activated:connect(function()
    local choose = math.random(1,2)
    canattack.Value = true
    if choose == 1 then
        local swing1animation = script.Parent.Parent.Humanoid:LoadAnimation(script.Punch1)
        swing1animation:Play()
        wait(.2)
        swingsound:Play()
        script.delay.Value = true
        script.cooldown.Disabled = false
    elseif choose == 2 then
        local swing2animation = script.Parent.Parent.Humanoid:LoadAnimation(script.Punch2)
        swing2animation:Play()
        wait(.2)
        swingsound:Play()
        script.delay.Value = true
        script.cooldown.Disabled = false
   end
end)
end

Put a Server Script inside the Main Script called 'cooldown' and make sure it's disabled. The cooldown can help set the delay back to false giving a pseudo-cooldown for your Main Script.

-- Second Script
if script.Parent.delay.Value == true then
	wait(.5)
	script.Parent.delay.Value = false
	script.Disabled = true
end

3 comments

0

lol get moderated boi

0

very weird way of doing the problem, you should use spawn() instead

0

well we're all different aren't we.

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