Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Why is damage being done without me clicking?

Asked by 5 years ago

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)

0
Make sure to post the script inside of a Script widget. superawesome113 112 — 5y
0
When someone touches your left or right arm, that person take 3.5 damage. User#25270 0 — 5y
0
Also, the sword itself, doesn't deal damage when clicked, it only loads the animation. User#25270 0 — 5y
0
I have some experience with punching scripts, but could you put this in a lua block or whatever it's called? Just click edit below the comments and click on lua at the top, and it will pop up a bunch of ~'s. Put your code in between those to make it easier on me. Knineteen19 307 — 5y

1 answer

Log in to vote
0
Answered by 5 years ago

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)
Ad

Answer this question