Damaging the player after they click something?

I have a part that gives the player a tool, and it does that just fine. However, I want it to be so that, when the player clicks this part, they take 10 damage every few seconds or so (in the script I’ll provide it says 3 but I’m likely to change it later) until they lose all 100 health.

I’m pretty sure I’ve got it right, but I think I’ve messed up somewhere since it doesn’t work. I think it’s something to do with the fact I’m trying to call the player’s Humanoid from the MouseClick function, but I don’t really know.

I’m not getting any console errors from this, so anyone know what’s up?
(If there’s an easier way to do this do let me know, as well.)

local Part = script.Parent
local ClickDetector = Part:WaitForChild("ClickDetector")

ClickDetector.MouseClick:connect(function(Player)
	if Player.Parent:FindFirstChild("Humanoid") ~= nil then
		local char = Player.Parent
		char:WaitForChild('Humanoid').Health -= 10
		wait(3)
		char:WaitForChild('Humanoid').Health -= 10
		wait(3)
		char:WaitForChild('Humanoid').Health -= 10
		wait(3)
		char:WaitForChild('Humanoid').Health -= 10
		wait(3)
		char:WaitForChild('Humanoid').Health -= 10
		wait(3)
		char:WaitForChild('Humanoid').Health -= 10
		wait(3)
		char:WaitForChild('Humanoid').Health -= 10
		wait(3)
		char:WaitForChild('Humanoid').Health -= 10
		wait(3)
		char:WaitForChild('Humanoid').Health -= 10
		wait(3)
		char:WaitForChild('Humanoid').Health -= 10
		wait(3)
		char:WaitForChild('Humanoid').Health -= 10
	end
end)

you’re defining char as the player’s parent, when the character is an object within the player

so instead it should be

local char = Player.Character

the player’s parent is playerservice

and yeah the simpler way to do this is using a loop

ClickDetector.MouseClick:connect(function(Player:Player)
	local char = Player.Character
	local hum = char:FindFirstChild('Humanoid')
	if hum == nil then return end
	
	repeat
		hum:TakeDamage(10) --amount it damages per loop
		task.wait(3) --time it waits per loop
	until hum.Health <= 0
end)
1 Like

Thanks much!

This:

didn’t work for me, which is still confusing me a little, but it was probably due to the way I spaghetti-coded the script as a whole.

The loop worked, though. I’m still figuring them out, so thanks a bunch.