I have no idea why it’s not stopping.
This is the local script placed inside of the tool.
Please help me with this, or show me better/more effecient ways to change the animations for when a tool is equipped.
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
local WeaponConfig = require(ReplicatedStorage.Modules.Configs:WaitForChild("WeaponConfig"))
local Remotes = ReplicatedStorage:WaitForChild("Remotes").Events
local PlayAnimationRemote = Remotes:WaitForChild("PlayAnimation")
local HitboxRemote = Remotes:WaitForChild("Hitbox")
local tool = script.Parent
local player = Players.LocalPlayer
local weaponData = WeaponConfig[tool.Name]
if not weaponData then return end
local toolType = tool:GetAttribute("Type")
local debounceTime = weaponData.DebounceDuration
local debounce = false
local ActiveTracks = {}
local moving = false
local updateConnection
local attacking = false
local function playAnim(char, id, name, priority)
local humanoid = char:FindFirstChildOfClass("Humanoid")
if not humanoid then return end
local animator = humanoid:FindFirstChildOfClass("Animator")
if not animator then
animator = Instance.new("Animator")
animator.Parent = humanoid
end
if ActiveTracks[name] then
ActiveTracks[name]:Stop()
ActiveTracks[name]:Destroy()
end
local anim = Instance.new("Animation")
anim.AnimationId = "rbxassetid://" .. tostring(id)
local track = animator:LoadAnimation(anim)
track.Priority = priority or Enum.AnimationPriority.Movement
track.Name = name
track:Play()
ActiveTracks[name] = track
return track
end
local function stopAnim(name)
if ActiveTracks[name] then
ActiveTracks[name]:Stop()
ActiveTracks[name]:Destroy()
ActiveTracks[name] = nil
end
end
local function stopAllAnims()
for name, _ in pairs(ActiveTracks) do
stopAnim(name)
end
end
local function updateMovementAnim()
if attacking then return end
local char = player.Character
if not char then return end
local humanoid = char:FindFirstChildOfClass("Humanoid")
if not humanoid then return end
local speed = humanoid.MoveDirection.Magnitude
if speed > 0.1 then
if not moving then
moving = true
if tool.Parent == char then
playAnim(char, weaponData.Animations.RunWithSword, "Move", Enum.AnimationPriority.Movement)
else
playAnim(char, weaponData.Animations.RunWithoutSword, "Move", Enum.AnimationPriority.Movement)
end
end
else
if moving then
moving = false
if tool.Parent == char then
playAnim(char, weaponData.Animations.IdleWithSword, "Idle", Enum.AnimationPriority.Idle)
else
playAnim(char, weaponData.Animations.IdleWithoutSword, "Idle", Enum.AnimationPriority.Idle)
end
end
end
end
tool.Equipped:Connect(function()
local char = player.Character or player.CharacterAdded:Wait()
moving = false
updateMovementAnim()
updateConnection = RunService.RenderStepped:Connect(updateMovementAnim)
end)
tool.Unequipped:Connect(function()
stopAllAnims()
if updateConnection then
updateConnection:Disconnect()
updateConnection = nil
end
moving = false
attacking = false
end)
tool.Activated:Connect(function()
if debounce then return end
debounce = true
task.delay(debounceTime, function()
debounce = false
end)
local char = player.Character or player.CharacterAdded:Wait()
attacking = true
PlayAnimationRemote:FireServer(tool, toolType)
task.delay(weaponData.DebounceDuration or 0.5, function()
attacking = false
end)
end)
Any help is appreciated!