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

Sword tool after touching a dummy and walking away still deals damage, how to fix that?

Hello, devs!

So title might be hard to understand, but I will explain everything right now. So I was making a sword tool that deals damage when you touch dummy and you activate tool (Mouse Click). My problem is if that if your tool will touch dummy and you will walk away and click your mouse, dummy will still get damaged. I also made string value that stores your name when you equip your tool and it works.

CODE:

script.Parent.Parent.Equipped:Connect(function()
	local player = script.Parent.Parent.Parent
	local debounce = false
	script.Parent.Parent.Owner.Value = player.Name
	script.Parent.Touched:Connect(function(hit)
		script.Parent.Parent.Activated:Connect(function()
			if hit.Name ~= script.Parent.Parent.Owner.Value then
				if hit.Parent:FindFirstChild("Humanoid") then
					if debounce == false then
						debounce = true
						hit.Parent:FindFirstChild("Humanoid").Health = hit.Parent:FindFirstChild("Humanoid").Health - 50
						wait(1)
						debounce = false
					end
				end
			end
		end)
	end)
end)

ERRORS:

No errors

DETAILS:

Sword Tool located in workspace, script located in part that is located in tool (everything is welded), owner name string value located in tool.

3 comments

0

I recommend not using the touched event. You should use magnitude with the Dot product.

0

What do you mean? I have no idea how to use magnitude...

0

Magnitude can detect how far an object is from another object, I recommend checking this out: https://scriptinghelpers.org/questions/14617/how-do-i-use-magnitude

1 answer

you have your events connected in the wrong order

Touched should be connected to when the tool is swung, right now you have it set so that Activated is connected to when the tool is touched

it doesn't really make sense that you'd touch someone with your sword, then after you've touched them, you'd have to click to damage them

local tool = script.Parent.Parent -- use variables pls

tool.Equipped:Connect(function()
    local player = tool.Parent
    local debounce = false
    script.Parent.Parent.Owner.Value = player.Name
end)

tool.Activated:Connect(function() -- there's no reason to connect to Activated when the tool is equipped, how is somebody going to activate the tool when nobody is holding it
    local hasTouchedEnemy = false -- a debounce is kind of unnecessary for this, you can just check if they've already hit something
    local touchedConnection = script.Parent.Touched:Connect(function(hit)
        if hit.Parent:FindFirstChild("Humanoid") and hit.Parent.Name ~= tool.Owner.Value and not hasTouchedEnemy then -- you only need one if statement for this
            hasTouchedEnemy = true
            hit.Parent.Humanoid.Health -= 50 -- you already checked if the parent has a humanoid, so why use FindFirstChild again
            -- compound operators winning btw
        end
    end)
    wait(1)
    touchedConnection:Disconnect() -- no reason to keep the event connection around since the player is done swinging
end)

in general you should try to avoid nested event connections unless you're going to disconnect them later, since they're a suuuuper easy way to introduce a memory leak

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