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

Why won't the animation play when I press shift?

Asked by 5 years ago

Hello all! I'm having an issue with a script I'm making.. To give you some insight, I want it when I press "LeftShift" it plays an animation, BUT it needs to check if the tool is equipped(M4), if the tool isn't equipped, then when you press Left Shift nothing happens. This is a Local Script inside of StarterCharacterScripts:

local player = game.Players.LocalPlayer
local character = player.Character or script.Parent
local Humanoid = character.Humanoid
local tool = game.StarterPack.M4 
local UserInputService = game:GetService('UserInputService')
local AnimationID = 'rbxassetid://033456263'

local Key = "LeftShift"
UserInputService.InputBegan:Connect(function(Input, IsTyping)
    if IsTyping then return end
    if Input.KeyCode == Enum.KeyCode[Key] then
        local Animation = Instance.new("Animation")
        Animation.AnimationId = AnimationID
        local LoadAnimation = Humanoid:LoadAnimation(Animation)
        tool.Equipped:Connect(function()
            LoadAnimation:Play()
        end)
    end
end)
0
When you press Left Shift, it listens to Equipped; only when that fires it will play the animation. Instead, check if the tool is equipped when you press Left Shift, then play the animation. pidgey 548 — 5y
0
Ok, thanks I'll try that. Mrmonkeyman120 65 — 5y
0
Didn't work.. Mrmonkeyman120 65 — 5y

1 answer

Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

Hello, so what you should've done was added the equipped event first then after the player presses the key the animation will play. I hope this was what you were looking for :D

--[[ Script ]] local player = game.Players.LocalPlayer local character = player.Character or script.Parent local Humanoid = character.Humanoid local tool = game.StarterPack.M4 local UserInputService = game:GetService('UserInputService') local AnimationID = 'rbxassetid://033456263'

tool.Equipped:Connect(function() local Key = "LeftShift" UserInputService.InputBegan:Connect(function(Input, IsTyping) if IsTyping then return end if Input.KeyCode == Enum.KeyCode[Key] then local Animation = Instance.new("Animation") Animation.AnimationId = AnimationID local LoadAnimation = Humanoid:LoadAnimation(Animation) LoadAnimation:Play() end end) end)

Ad

Answer this question