Title explains it all. I know how to use the ClickDetector and how to input a function, but I don't know how I would make a function that damages the player every 2 or so seconds. Can someone help and explain to me what I need to use?
✓ ANSWER ACCEPTED
How to make it so that when a player presses on a part, they take damage until their health is at 1?
2 answers
✓ Accepted answer
Here.
local clickdetector = script.Parent.ClickDetector
clickdetector.MouseClick:Connect(function(plr)
repeat
wait(0.1)
plr.Character.Humanoid.Health = plr.Character.Humanoid.Health - 1
until plr.Character.Humanoid.Health == 1
end)
When it makes it damage you every 2 seconds the player heals so it's not possible to get to the goal, so I changed it.
Make sure this is a server script inside the click detector.
local clickDetector = script.Parent
clickDetector.MouseClick:Connect(function(player) --get the player by writing a parameter within the function
local character = player.Character
local humanoid = character.Humanoid
if not humanoid.Health > 0 then --if player is not alive then stop the script
return
end
repeat --repeats whatever is under the loop until given condition is met which is in this case when the humanoid's health is 1
humanoid:TakeDamage(1) --you can change the damage here
wait() --you can change the rate of damage here
until humanoid.Health == 1
end)
This discussion is preserved as it was. New answers, comments, and votes are closed.

2 comments
Thank you!
You’re welcome.