Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Arm damage for punching tool still damaging when not clicking?

So I got this model were you can insert your animations when clicked and does damage, but when I'm not using the tool or not clicking it still does damage to the player, In the tool, there's no handle. There is a boolvalue inside the tool which is named "CanAttack", please fix this!

Server 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(10) -- 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(10) -- u can change this value
			wait(.5)
			canattack.Value = false
		end
	end
end)

1 answer

This is because the LocalScript will immediately run the Code, I know this is quite bland, obvious too, however it actually has no debounce. A Debounce is something that'll stop a scope of Lua from running if a certain Condition is met. Assuming you already know this though, as you've used it in the Touched Listener.

But that's where the problem I'm explaining is, you didn't debounce the Listener itself, when the Tool isn't equipped. This will allow the Code to run immediately, and continue to preform it's work.


This is really easy to do though, all you have to do is wrap the Touched Event into the Signal Equipped of Tool. This Signal fires a Listener when Equipped, and yeilds the Listener when not.

Do this for your Script:

tool.Equipped:Connect(function() -- Use ':Connect'
   larm.Touched:Connect(function()

   end)
   -- Etcetera
end)

Hope this helps you out! Remember to accept this answer if so! Have a nice day

5 comments

0

Hmm.. still doesn't work, there were instructions on putting it somewhere. I put it inside the server script but still broken.

0

*were no instructions.

0

and um.. still doesn't work.

0

This should be a LocalScript inside the tool

0

You should also include the rarm

This discussion is preserved as it was. New answers, comments, and votes are closed.