Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by
quinzt 201 Moderation Voter
4 years ago

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

1
Instead of "for i = 100, 0, 1", do like this "for i = 100, 0, -1" Block_manvn 395 — 4y
0
thanks lol im an idiot quinzt 201 — 4y

2 answers

Log in to vote
0
Answered by
iuclds 720 Moderation Voter
4 years ago
Edited 4 years ago

A better way is to use tweenservice

local time = 8
game:GetService('TweenService'):Create(plr.Character.Humanoid,TweenInfo.new(time),{Health = 0}):Play()
Ad
Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

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

0
well the script is inside the click detector, thus why i wrote script.parent. i shouldve been more clear with that quinzt 201 — 4y
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 IAmNotTheReal_MePipe 418 — 4y

Answer this question