How would I make the player lose health continously

Hello Users of devforum. I’ve made this script where the player will go fast when he clicks this part.
I want to find a way to make it how the player can lose their health while this speed effect is on.

local player = game.Players.LocalPlayer

--Increase speed by X when clicked.

script.Parent.Parent.ClickDetector.MouseClick:Connect(function(plr)
	local char = plr.Character
	local h = char:WaitForChild("Humanoid")
	if char then
		h.WalkSpeed = 25
	end
end)

Any suggestions?

1 Like

Did you add prints to debug it? Unless I’m mistaken, you should have been able to deduce that the script wasn’t running at all. LocalScripts don’t run in Workspace (unless they are a member of Character) and so this LocalScript needs to be relocated to StarterCharacterScripts, and use player instead of plr

Well this is in a server script actually.
image

Add Humanoid:TakeDamage() after you set walkspeed:

script.Parent.Parent.ClickDetector.MouseClick:Connect(function(plr)
	local char = plr.Character
	if char then
		local h = char:WaitForChild("Humanoid")
		h.WalkSpeed = 25
		while h.WalkSpeed = 25 do
			h:TakeDamage(1)
			task.wait(0.5)
		end
	end
end)

Also, in your original script, you used a WaitForChild() before you checked for char. This will either error or put your script in an infinite yield.

Oh my bad, I didn’t realize this was working as-is.
You’ll want something like this.

while h.WalkSpeed == 25 do
    h.Health -= 1
    task.wait(1)
end
1 Like

Haven’t see neither yet so far

Unsure what you mean by that, but I’m glad it got solved. For a suggestion, if you wanted to not take damage while you have a forcefield, use TakeDamage() instead.