Ad
Log in to vote
0

How to make it so that when a player presses on a part, they take damage until their health is at 1?

Asked by
Skyyrn 1
8 days ago

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?

2 answers

Log in to vote
0
Answered by 8 days ago

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.

0
Thank you! Skyyrn 1 — 8d
0
You’re welcome. squidiskool 200 — 8d
Ad
Log in to vote
0
Answered by 8 days ago

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)

Answer this question