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

How can i make the health of the player slowly drain via a for loop?

i am trying to make a brick that when clicked will slowly drain your health. the code goes as follows:

script.Parent.MouseClick:Connect(function(plr)
	local h = plr.Character.Humanoid
	for i = 100, 0, 1 do
		h.Health = i
	end
end)

it should work fine from what i understand, but whenever i click on the brick nothing happens. I'm pretty sure that it doesnt have anything to do with the variables because whenever i type:

script.Parent.MouseClick:Connect(function(plr)
	local h = plr.Character.Humanoid
		h.Health = 0
end)

it works, so the variables arent the problem.

Help would be appreciated

2 comments

1

Instead of "for i = 100, 0, 1", do like this "for i = 100, 0, -1"

0

thanks lol im an idiot

2 answers

✓ Accepted answer

A better way is to use tweenservice

local time = 8
game:GetService('TweenService'):Create(plr.Character.Humanoid,TweenInfo.new(time),{Health = 0}):Play()

You need to add a clickDetector to your part. You also will want to edit your for loop

script.Parent.ClickDetector.MouseClick:Connect(function(plr)
	  local h = plr.Character.Humanoid
	  for i = 100, 0, -1 do
	        h.Health = i
	  end
end)

This should fix your program

2 comments

0

well the script is inside the click detector, thus why i wrote script.parent. i shouldve been more clear with that

0

Ok, then get rid of the '.ClickDetecter' part of my code. Your for loop was incorrect, causing you to have errors. My for loop says 'for i = 100,0,-1' while yours says 'for i = 100,0,1' If you change your loop to my for loop, your script should work

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