Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How to start an animation when a button is pushed?

Asked by
sheepposu 561 Moderation Voter
5 years ago

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

Log in to vote
0
Answered by 5 years ago

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.

0
I see... Thanks! sheepposu 561 — 5y
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. sheepposu 561 — 5y
0
you should only have it if you plan on using it :/ theking48989987 2147 — 5y
0
Never Mind, I just made a script in ServerScriptService and told it to change the sit animationId to the biking animationId sheepposu 561 — 5y
0
Yes, the animation's priority will determine if it is playable depending on the other animations being used SerpentineKing 3885 — 5y
Ad

Answer this question