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

How to start an animation when a button is pushed?

I have this code that I put in a local script inside a seat. It's supposed to launch when they click left shift while sitting on it. I can't seem to figure out why it won't work. Thanks in Advance!

local uis = game:GetService("UserInputService")
local player = game.Players.LocalPlayer
local ani = script.Parent.Animation
local aT = player.Character.Humanoid:LoadAnimation(ani)


uis.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.LeftShift then
		aT:Play()
	end
end)

uis.InputEnded:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.LeftShift then
		aT:Play()
	end
end)

1 answer

✓ Accepted answer

Local Script

local uis = game:GetService("UserInputService")
local player = game.Players.LocalPlayer
local ani = script.Parent.Animation
repeat wait() until player.Character ~= nil and workspace:FindFirstChild(player.Name) ~= nil
local aT = player.Character.Humanoid:LoadAnimation(ani)

uis.InputBegan:Connect(function(input, gameProcessedEvent)
	if input.KeyCode == Enum.KeyCode.LeftShift then
		aT:Play()
	end
end)

uis.InputEnded:Connect(function(input, gameProcessedEvent)
	if input.KeyCode == Enum.KeyCode.LeftShift then
		aT:Stop()
	end
end)

Make sure to have the "gameProcessEvent" at the end, you have 2 play animations, when the InputEnded should prob be for stopping, you need to wait under the player.Character is loaded before loading the animation.

5 comments

0

I see... Thanks!

0

The only problem is it doesn'y work. Could it have something to do with the fact that my character is sitting down. Would making the animation priority - "Action", fix it.

0

you should only have it if you plan on using it :/

0

Never Mind, I just made a script in ServerScriptService and told it to change the sit animationId to the biking animationId

0

Yes, the animation's priority will determine if it is playable depending on the other animations being used

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