Hi. Sorry for the confusing title, I'm making a punch tool but the animation only plays whenever it's near an enemy. How can I fix this? Thanks in advance.
(Sorry for the confusing script)
Here's the punch tool's script:
-- ---------------
local tool = script.Parent
local hitsound = tool:WaitForChild("Hit")
local hit1sound = tool:WaitForChild("Hit2")
local swingsound = tool:WaitForChild("Swing")
local canattack = tool:WaitForChild("CanAttack")
local plr = tool.Parent.Parent
local debounce = false
local char = plr.Character or plr.CharacterAdded:wait()
local root = plr.Character:WaitForChild("HumanoidRootPart")
local char
local hum
local idle
local swing1anim
local swing2anim
local ray
local hitposition
local hit
-- -------------
tool.Equipped:connect(function() -- tool equipped
swing1animation = swing1anim or script.Parent.Parent.Humanoid:LoadAnimation(script.Punch1)
swing2animation = swing2anim or script.Parent.Parent.Humanoid:LoadAnimation(script.Punch2)
char = char or plr.Character or plr.CharacterAdded:wait()
hum = hum or char:WaitForChild("Humanoid")
idle = idle or hum:LoadAnimation(script.Idle)
idle:Play()
idle.Looped = true
end)
tool.Activated:Connect(function(player) -- tool activated
wait(.133)
script.Parent.Parent:WaitForChild("Part").Touched:connect(function(hit) -- if enemy touching player's hitbox, it can punch
if debounce == false then
local player = game.Players:GetPlayerFromCharacter(char)
local mark = script.Mark
local kb = script.KB
local choose = math.random(1,2)
if hit then -- if hit
local humanoid = hit.Parent:FindFirstChild("Humanoid") or hit.Parent:FindFirstChild("HumanoidE")
if not humanoid ~= nil then
humanoid = hit.Parent:FindFirstChild("Humanoid") or hit.Parent:FindFirstChild("HumanoidE")
end
if humanoid ~= nil then
if choose == 1 then
debounce = true
swing1animation:Play() -- play the animation
swingsound:Play()
wait(.25)
hitsound:Play()
mark.Value = true
kb.Value = true
humanoid:TakeDamage(9) -- deal damage to the enemy
wait(.33)
swingsound:Stop()
hitsound:Stop()
swing1animation:Stop()
debounce = false
if hit.Parent:FindFirstChild(player.Name) then
debounce = false
return end
local creator = Instance.new("StringValue",hit.Parent)
creator.Name = player.Name
creator.Value = player.Name
game.Debris:AddItem(creator,7)
debounce = false
elseif choose == 2 then
debounce = true
swingsound:Play()
swing2animation:Play() -- play the animation
wait(.25)
hit1sound:Play()
mark.Value = true
kb.Value = true
humanoid:TakeDamage(9) -- deal damage to the enemy
wait(.33)
swingsound:Stop()
hit1sound:Stop()
swing2animation:Stop()
debounce = false
if hit.Parent:FindFirstChild(player.Name) then
debounce = false
return end
local creator = Instance.new("StringValue",hit.Parent)
creator.Name = player.Name
creator.Value = player.Name
game.Debris:AddItem(creator,7)
debounce = false
end
else
return
end
end
end
end)
end)
tool.Deactivated:connect(function() -- tool deactivated
canattack.Value = true
end)
tool.Unequipped:Connect(function() -- tool unequipped
idle:Stop()
end)
