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

how do i make a working Keybinding animation script?

Hello, so here is the script that i made, after you press R it should play animation when you have your arms crossed, until you press R again to stop the animation. But instead it started playing the animation, and even if i pressed R again it didnt stop. i tried putting print "case a" and print "case b" to both if functions, and in output when i pressed R it only showed me case a, basically the first "if". Also, can the reason be that my animation is looped?(also i already tried writing anim:Stop() instead of anim:Destroy(), it still didnt help). Thank you all in advance.

Player = game.Players.LocalPlayer
mouse = Player:GetMouse()
local anim = nil
--Put me in StarterGui
mouse.KeyDown:connect(function(key)
key = key:lower()
	if key == "r" then
		anim = Player.Character.Humanoid:LoadAnimation(script.TestAnim)
		anim:Play()
	elseif key == "r" then
		if anim ~= nil then
			anim:Destroy()
		end
	end
end)

2 answers

✓ Accepted answer

Here's I do my animations. It uses (to me) simpler steps.

wait(-1)
Player = game.Players.LocalPlayer
local Character = Player.Character
local Humanoid = Character.Humanoid
local animID ='rbxassetid://5161042536' 
local UIS = game:GetService("UserInputService")
local db=true

UIS.InputBegan:Connect(function(input)
    if input.KeyCode == Enum.KeyCode.E and db == true then
          local Animation = Instance.new("Animation")
        Animation.animID = animID
        local LoadAnimation = Player.Character.Humanoid:LoadAnimation(Animation)

    LoadAnimation:Play()
    else
        db = true
        print("F")

    end

end)

Pretty simple. Uses user input service.

1 comment

0

Hello, I have an error "Humanoid is not a valid member of the model

Hello, I have an error "Humanoid is not a valid member of the model"

Player = game.Players.LocalPlayer
local Character = Player.Character
local Humanoid = Character.Humanoid
local animID ='rbxassetid://5161042536' 
local UIS = game:GetService("UserInputService")
local db=true

UIS.InputBegan:Connect(function(input)
    if input.KeyCode == Enum.KeyCode.E and db == true then
		  local Animation = Instance.new("Animation")
		Animation.animID = animID
		local LoadAnimation = Humanoid:LoadAnimation(Animation)
	
	LoadAnimation:Play()
	else
		db = true
		print("F")

    end

end)

2 comments

0

I edited it.

0

Thank you for your help! had to a little edit it in order, for animation to be disabled by the same button, but now it works!

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