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

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

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)

3 comments

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.

0

Ok, thanks I'll try that.

0

Didn't work..

1 answer

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)

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