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

How to make a key bind animation that will play until you press key again?

Hi, so I have a gui textbutton with a local script and an animation. When you click the textbutton an animation play and continues to play until you press the textbutton again, making the animation stop and you go back to idle animation.

I also need to make a keybinable one for computer users. The script below is for the textbutton gui, but does anyone know how to make it as a key?

local btn = script.Parent
local plr = game.Players.LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait()
local hum = char:WaitForChild("Humanoid",10) --gets and waits for player
local CanEmote = true
local CoolDown = 0 
local debounce = false -- Debounce
local dance = hum:LoadAnimation(script:FindFirstChild("Anim1")) --this is currently a gui script, so this detects an animation in the textbutton
 
 
btn.MouseButton1Down:Connect(function() --detects when player presses the button, but i need it to be a key
    if debounce == false then
        debounce = true
        if CanEmote == true then
            hum.JumpPower = 0 -- this makes it so the player can't move while the animation plays
            hum.WalkSpeed = 0
            CanEmote = true
            dance:Play() -- animation
        end
    elseif debounce == true then --detects if u press it again, making the animation stop and putting you back to idle
        debounce = false
        dance:Stop()
        hum.WalkSpeed = 16
        hum.JumpPower = 50
    end
end)

Let's say you press "R" the animation plays, continue to play, and then stops if you press "R" again. does anyone know how to do this? Any help is appreciated. I just what to understand this since I'm a beginner at making my own scripts. Thank you.

1 answer

✓ Accepted answer

First off, to get the key you can go two ways: UserInputService or Mouse.KeyDown.

Before, you'd use Mouse.KeyDown , but since that is (basically) depreciated, we'll use UserInputService .

game:GetService("UserInputService").InputBegan:Connect(function(key, gameProcessed)
	if key.KeyCode == enum.KeyCode.R then
		print("You pressed 'R' !")
	end
end)

This snipped of code will print "You pressed 'R' !" every time you pressed R.

Utilizing this function, we'd be able to play an animation with it like this in your main script.

and lastly, to make the animation repeat until you press the key again, all you need to do is make the animation looped .

local btn = script.Parent
local plr = game.Players.LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait()
local hum = char:WaitForChild("Humanoid")

local CanEmote = true
local CoolDown = 0 
local debounce = false
local dance = hum:LoadAnimation(script:FindFirstChild("Anim1"))

dance.Looped = true

game:GetService("UserInputService").InputBegan:Connect(function(key, gameProcessed)
	if key.KeyCode == enum.KeyCode.R then
		if debounce == false then
			debounce = true
			if CanEmote == true then
				hum.JumpPower = 0
				hum.WalkSpeed = 0
				CanEmote = true
				dance:Play()
			end
		elseif debounce == true then
			debounce = false
			dance:Stop()
			hum.WalkSpeed = 16
			hum.JumpPower = 50
		end
	end
end)

^ Finished code

Hope this helped!

2 comments

0

So, my script is currently in a textbutton gui that also has a animation in it. Where would I put this one, and would I also put the animation with it? Also thank you for answering and explaining, I like to understand these things.

0

Never mind I got it to work. Thank you so much! This helped a lot, thank you!

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