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

why doesnt the animation stop playing in this script when the key is depressed?

local userInputService = game:GetService("UserInputService")

local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded
local Anim = Instance.new("Animation")

--this is for any inputs at all

userInputService.InputBegan:Connect(function(input,GameProcessedEvent)
	if input.KeyCode == Enum.KeyCode.E then
		local humanoid = Player.Character.Humanoid
		Anim.AnimationId = "rbxassetid://986231579"
		local playAnim = humanoid:LoadAnimation(Anim)
		script.Parent.Handle.Transparency = 0.5
		if script.Parent.Handle.Transparency == 0.5 then
			script.Parent.Sound:Play()
			playAnim:Play()
		end
	end
end)



--this is for when the e key is pressed down
--the gameProcessedEvent is so the game recognizes the event i assume its for making server events with a local script

userInputService.InputEnded:Connect(function(input,GameProcessedEvent)
	if input.KeyCode == Enum.KeyCode.E then
		local humanoid = Player.Character.Humanoid
		Anim.AnimationId = "rbxassetid://986231579"
		local playAnim = humanoid:LoadAnimation(Anim)
		script.Parent.Handle.Transparency = 1
		playAnim:Stop()
	end
end)

userInputService.InputEnded:Connect(function(input,GameProcessedEvent)
	if input.KeyCode == Enum.KeyCode.E then
	script.Parent.Sound:Stop()
	end
end)
--this is for when the e key is let go of

2 comments

0

Please format the script

0

my bad

2 answers

✓ Accepted answer

You are trying to make a new animation play when the player releases E instead of stop the currently playing one. To fix this you need to place a variable at the top to store the playing animation and stop it when the player releases the key as so:

local userInputService = game:GetService("UserInputService")

local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded
local Anim = Instance.new("Animation")
local humanoid = Player.Character.Humanoid -- Declare the Humanoid here since its before the InputBegan event
local playAnim = humanoid:LoadAnimation(Anim) --Declare the animation here
Anim.AnimationId = "rbxassetid://986231579" -- Set the ID here since its before the InputBegan event
--this is for any inputs at all

userInputService.InputBegan:Connect(function(input,GameProcessedEvent)
    if input.KeyCode == Enum.KeyCode.E then
        script.Parent.Handle.Transparency = 0.5
        if script.Parent.Handle.Transparency == 0.5 then
            script.Parent.Sound:Play()
            playAnim:Play() --Start the animation we stored at the top
        end
    end
end)



--this is for when the e key is pressed down
--the gameProcessedEvent is so the game recognizes the event i assume its for making server events with a local script

userInputService.InputEnded:Connect(function(input,GameProcessedEvent)
    if input.KeyCode == Enum.KeyCode.E then
        script.Parent.Handle.Transparency = 1
        playAnim:Stop() --Stop the animation we stored at the top
    end
end)

userInputService.InputEnded:Connect(function(input,GameProcessedEvent)
    if input.KeyCode == Enum.KeyCode.E then
    script.Parent.Sound:Stop()
    end
end)
--this is for when the e key is let go of

2 comments

0

when i run this script i receive an iutput error that reads as follows "Players.coda6000.Backpack.Explode.LocalScript:6: attempt to index nil with 'Humanoid'"

0

It seems that the humanoid isnt found in the character

place

local playAnim = humanoid:LoadAnimation(Anim) 

on the top because your loading a new animation everytime you click E and stop clicking E so its not the same animation that your trying to stop

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