I didn’t have this issue before, please help!
(this is all being done through a module script)
^idle animation being used on another player (i think it’s whoever joins first)
--{{ VARIABLES }}--
local animationSwing = Instance.new("Animation")
local animationIdle = Instance.new("Animation")
animationSwing.AnimationId = "rbxassetid://17007907725"
animationIdle.AnimationId = "rbxassetid://17006666497"
local idle
local debounce = false
local dbdmg = false
local equipped = false -- Track if the weapon is equipped
local Players = game:GetService("Players")
module.Weapon = function()
game.Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(character)
-- Idle Animation
local function startIdle()
if not idle then -- Only load idle animation if it's not already loaded
idle = character.Humanoid:LoadAnimation(animationIdle)
idle.Priority = Enum.AnimationPriority.Action
idle.Looped = true
end
idle:Play()
end
local function stopIdle()
if idle and not equipped then -- Stop idle animation only if not equipped
idle:Stop()
end
end
-- Swing Animation
local function performSwing(tool)
if not debounce then
debounce = true
if idle then
idle:Stop()
end
local humanoid = character:FindFirstChildOfClass("Humanoid")
local animator = humanoid and humanoid:FindFirstChild("Animator")
if animator then
local swingAnimation = animator:LoadAnimation(animationSwing)
swingAnimation:Play()
local Hitbox = tool.Hitbox
local hitCharactersTABLE = {}
Hitbox.Touched:Connect(function(hit)
if not dbdmg and hit.Parent:FindFirstChildOfClass("Humanoid") and debounce == true then
dbdmg = true
local hitCharacter = hit.Parent:FindFirstChildOfClass("Humanoid")
table.insert(hitCharactersTABLE, hitCharacter)
if hitCharactersTABLE.hitCharacter == hitCharactersTABLE.hitCharacter then
hitCharacter:TakeDamage(BaseWeaponDamageValues.SwordDamage)
else
end
end
end)
swingAnimation.Ended:Connect(function()
debounce = false -- Reset for animation
hitCharactersTABLE = {}
if equipped then
startIdle() -- Start idle animation only if still equipped
end
dbdmg = false -- Reset for damage
end)
tool.Unequipped:Connect(function()
if swingAnimation.IsPlaying then
swingAnimation:Stop()
debounce = false -- Reset debounce flag
end
end)
end
end
end
local function onToolAdded(tool)
tool.Activated:Connect(function()
performSwing(tool)
end)
tool.Equipped:Connect(function()
equipped = true
startIdle()
end)
tool.Unequipped:Connect(function()
equipped = false
stopIdle()
end)
end
-- Connect ChildAdded event to listen for tools being added to the character
character.ChildAdded:Connect(function(child)
if child:IsA("Tool") then
onToolAdded(child)
end
end)
-- Check existing tools in character's backpack
for _, tool in ipairs(character:GetChildren()) do
if tool:IsA("Tool") then
onToolAdded(tool)
end
end
end)
end)
end