im coding an r6 melee weapon (just for detail) and i’ve added an idle animation onto it… the idle animation itself is a looped animation, and it plays just fine!
i cant, however, get it to stop playing… :GetPlayingAnimationTracks returns as a nil value, and the script runs into an error once I call the track to :Stop()…
local tool = script.Parent
local m6d
local animation
local playa
script.Parent.equip.OnServerEvent:Connect(function(player)
playa = player
end)
tool.Equipped:Connect(function()
animation = script.idle
local hum = script.Parent.Parent:FindFirstChild("Humanoid")
local truck = hum:LoadAnimation(animation)
local a:Weld = script.Parent.Parent:FindFirstChild("Right Arm"):WaitForChild("RightGrip")
m6d = Instance.new("Motor6D")
m6d.Parent = script.Parent.Parent:FindFirstChild("Right Arm")
m6d.Name = "RightGrip"
m6d.Part0 = a.Part0
m6d.Part1 = a.Part1
m6d.C0 = a.C0
m6d.C1 = a.C1
a:Destroy()
truck:Play()
end)
tool.Unequipped:Connect(function()
print(playa)
animation = script.idle
local hum = playa.Character:FindFirstChild("Humanoid")
local truck = hum.Animator:GetPlayingAnimationTracks()
m6d:Destroy()
truck:Stop()
end)
im sorry if this is a really simple problem, i’m new to scripting and this is my first devforum post…
thank you in advance, though!
Its better to not use looped animations, set the looped property to false and keep playing the animation until he moves (or whenever you want) and stop after, better to use runService and make it wait until the animation is ended, feel free to ask for a code to understand what i meant!
Also you shouldn’t load the animation every time you equip the tool instead just load the Animation at the start of the script to prevent you from hitting the Animation limit.
Ok so to fix this you first Load the animation at the start of the script like this
local plr = game:GetService("Players").LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait()
local Hum = char:FindFirstChildOfClass("Humanoid") or char:WaitForChild("Humanoid")
local Animator = Hum:WaitForChild("Animator")
local ani = script.idle
local truck = Animator:LoadAnimation(ani)
This is written on phone so might have miss spelled some stuff
animation.looped = false --to force it un-looped
local runService = game:GetService("RunService")
local isPlaying = {} --a table so you can add as much animations as you want!
runService.renderStepped:Connect(function()
if not table.find(isPlaying , animation.Name) then
animation:Play() --check capitalization...
table.insert(isPlaying , animation.Name)
animation.Stopped:Connect(function()
table.remove(isPlaying , table.find(isPlaying , animation.Name))
end)
end
end)
im guessing that wouldnt work in a server script (which is what i have the script in currently)
what i used to get the humanoid (but only utilized it in the unequip function) was a remote event fired to the server from local script; do you think i could define player and character through that and the script you posted would still function the same way?
so what this does is that it unloops the animation, creates a table for animations, then… what exactly does the runservice function do? im sorry, imt rying to understand it
It keeps on running the animation, if it is NOT on the table then add it, play it and when it finishes then remove from table, you can add a break when you want to stop it (or another variable)