I have made a punching script. I created two dummies to test out the script. The scripts are inside a tool, and when you select the tool the animation plays. But, when I don't click and I'm next to one of the dummies the dummies take damage without me clicking. What I want fixed is when I click the dummies take damage, and not when I don't click. Can anyone help me?
Here is the damage script:
local tool = script.Parent local hitsound = tool:WaitForChild("Hit") local canattack = tool:WaitForChild("CanAttack") local plr = tool.Parent.Parent local char = plr.Character or plr.CharacterAdded:wait() local larm = char:WaitForChild("Left Arm") -- this can be changed im only using this for the R6 body type local rarm = char:WaitForChild("Right Arm") -- this can be changed im only using this for the R6 body type
larm.Touched:connect(function(hit) local hum = hit.Parent:FindFirstChild("Humanoid") if canattack.Value == false then if hum then canattack.Value = true hitsound:Play() hum:TakeDamage(3.5) -- u can change this value wait(.5) canattack.Value = false end end end)
rarm.Touched:connect(function(hit) local hum = hit.Parent:FindFirstChild("Humanoid") if canattack.Value == false then if hum then canattack.Value = true hitsound:Play() hum:TakeDamage(3.5) -- u can change this value wait(.5) canattack.Value = false end end end)
Here is the animation script:
local plr = game.Players.LocalPlayer local char = plr.Character or plr.CharacterAdded:wait() local tool = script.Parent local swingsound = tool:WaitForChild("Swing") local canattack = tool:WaitForChild("CanAttack")
tool.Equipped:connect(function() local idle = script.Parent.Parent.Humanoid:LoadAnimation(script.Idle) idle:Play() end)
tool.Activated:connect(function() local choose = math.random(1,2) canattack.Value = false if choose == 1 then local swing1animation = script.Parent.Parent.Humanoid:LoadAnimation(script.Punch1) swingsound:Play() swing1animation:Play() elseif choose == 2 then local swing2animation = script.Parent.Parent.Humanoid:LoadAnimation(script.Punch2) swingsound:Play() swing2animation:Play() end end)
tool.Deactivated:connect(function() canattack.Value = false end)
tool.Unequipped:connect(function() local hum = char:WaitForChild("Humanoid") for _,anim in pairs(hum:GetPlayingAnimationTracks()) do anim:Stop() end end)
For those who had a hard time reading the code
local tool = script.Parent local hitsound = tool:WaitForChild("Hit") local canattack = tool:WaitForChild("CanAttack") local plr = tool.Parent.Parent local char = plr.Character or plr.CharacterAdded:wait() local larm = char:WaitForChild("Left Arm") -- this can be changed im only using this for the R6 body type local rarm = char:WaitForChild("Right Arm") -- this can be changed im only using this for the R6 body type larm.Touched:connect(function(hit) local hum = hit.Parent:FindFirstChild("Humanoid") if canattack.Value == false then if hum then canattack.Value = true hitsound:Play() hum:TakeDamage(3.5) -- u can change this value wait(.5) canattack.Value = false end end end) rarm.Touched:connect(function(hit) local hum = hit.Parent:FindFirstChild("Humanoid") if canattack.Value == false then if hum then canattack.Value = true hitsound:Play() hum:TakeDamage(3.5) -- u can change this value wait(.5) canattack.Value = false end end end) Here is the animation script: local plr = game.Players.LocalPlayer local char = plr.Character or plr.CharacterAdded:wait() local tool = script.Parent local swingsound = tool:WaitForChild("Swing") local canattack = tool:WaitForChild("CanAttack") tool.Equipped:connect(function() local idle = script.Parent.Parent.Humanoid:LoadAnimation(script.Idle) idle:Play() end) tool.Activated:connect(function() local choose = math.random(1,2) canattack.Value = false if choose == 1 then local swing1animation = script.Parent.Parent.Humanoid:LoadAnimation(script.Punch1) swingsound:Play() swing1animation:Play() elseif choose == 2 then local swing2animation = script.Parent.Parent.Humanoid:LoadAnimation(script.Punch2) swingsound:Play() swing2animation:Play() end end) tool.Deactivated:connect(function() canattack.Value = false end) tool.Unequipped:connect(function() local hum = char:WaitForChild("Humanoid") for _,anim in pairs(hum:GetPlayingAnimationTracks()) do anim:Stop() end end)