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

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

Asked by 1 year ago
Edited 1 year ago
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
0
Please format the script Sabailuridze 126 — 1y
0
my bad coda6000 2 — 1y

2 answers

Log in to vote
1
Answered by 1 year ago

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
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'" coda6000 2 — 1y
0
It seems that the humanoid isnt found in the character Rogue_Ram 40 — 1y
Ad
Log in to vote
1
Answered by
Puppynniko 1059 Moderation Voter
1 year ago
Edited 1 year ago

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

Answer this question