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

attempt to index nil with :Stop() ?? (FIXED)

Asked by 3 years ago
Edited 3 years ago

For some reason, my script doesn't work, if anybody could fix this that would help! So I am making a weight lifting simulator game and I kinda want my animation to play when the tool is Equipped. Inside the tool.Equipped function everything works. But the tool.Unequipped function has a bug, it doesn't want to switch back the humanoids walk speed to 16, and plus the animation won't stop, something with the : Stop() function

local tool = script.Parent
local active = false
local anim = Instance.new("Animation")
local anim2 = Instance.new("Animation")
anim.AnimationId = "rbxassetid://5303291613"
anim2.AnimationId = "rbxassetid://5652791809"
local track
local track2
local gamepassId = 10042655
local player = game.Players.LocalPlayer
tool.Equipped:Connect(function()
    active = true
    track2 = script.Parent.Parent.Humanoid:LoadAnimation(anim2)
    track2.Priority = Enum.AnimationPriority.Action
    track2.Looped = false
    track2:Play()
    script.Parent.Parent.Humanoid.WalkSpeed = 0
end)
tool.Activated:Connect(function()
    track = script.Parent.Parent.Humanoid:LoadAnimation(anim)
    track.Priority = Enum.AnimationPriority.Action
    track.Looped = false
    track:Play()

end)
tool.Unequipped:Connect(function()
    if track2 then
        track:Stop()
        track2:Stop()
        active = false
        player.Character.Humanoid.WalkSpeed = 16
    end
end)

1 answer

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

It errors bc track is nil, and track is nil if the Activated event doesnt fire, you need to add another if statement to prevent nil:Stop()

like

 if track then
    track:Stop()
end
Ad

Answer this question