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

Where to add delay to this animation code?

My code is as follows:

wait(3)

local player = script.Parent.Parent.Name;
local Humanoid = game.Workspace[player];

	
	

function onKeyPress(inputObject, gameProcessedEvent)
		if inputObject.KeyCode == Enum.KeyCode.E then
		local test = Humanoid.Humanoid:LoadAnimation(game.Workspace.Animation);
		test:Play()
		wait(1)
	end
	
end
 
game:GetService("UserInputService").InputBegan:connect(onKeyPress)

Everything works fine, except that if you spam E you would be able to keep re-playing the animation without finishing its cycle. I want to add a wait() somewhere so the animation does not repeat until a certain time. What is the exact point to add it? Thanks.

Edit 1: My goodness, everything I post a question on here I manage to answer it almost immediately. If anyone has the same question, my code is as follows:

wait(3)

local player = script.Parent.Parent.Name;
local Humanoid = game.Workspace[player];

	
	

function onKeyPress(inputObject, gameProcessedEvent)
		if inputObject.KeyCode == Enum.KeyCode.E then
			if script.Name == "Script" then
		local test = Humanoid.Humanoid:LoadAnimation(game.Workspace.Animation);
		test:Play()
		script.Name = "waiter"
		wait(1.4)
		script.Name = "Script"
		end
	end
	
end
 
game:GetService("UserInputService").InputBegan:connect(onKeyPress)

1 answer

wait(3)

local player = script.Parent.Parent.Name;
local Humanoid = game.Workspace[player];
clicked = false;




function onKeyPress(inputObject, gameProcessedEvent)
        if inputObject.KeyCode == Enum.KeyCode.E and clicked == false then
            if script.Name == "Script" then
clicked = true
        local test = Humanoid.Humanoid:LoadAnimation(game.Workspace.Animation);
        test:Play()
        script.Name = "waiter"
        wait(1.4)
        script.Name = "Script"

	wait(10) -- This is like cooldown time + wait time above ^^
clicked = false
        end
    end

end

game:GetService("UserInputService").InputBegan:connect(onKeyPress)


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