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

How do you make a part to set health to a the value you've set it to?

Asked by
iiAceMo 50
4 years ago

So what I'm trying to say is that when you script a part to make it so that (for example) it sets your health value to 50 when you touch it, how do you make it so that if your health is below 50, you can't set it higher. Because what I'm trying to do is to decrease your health after the part goes from green to red after you touch it.

For example, you touch a part then it sets your health to 50(color orange), then you touch another part that would make your health 25(darker orange), how do you make it so that if your health is lower than 50 and when you press the part to make your health to 25, it wont go back to 50 after pressing the part that makes your health 50. Sorry if that looked pretty confusing, I would make it look simpler but I'm rushing while making this because I have to eat for dinner. Lol

0
Instead of setting the health, you should try just lowering it by a said amount. For example in the first part you do, humanoid.Health = humanoid.Health - 50, for the second part, same but with 25 for example. It isn't perfect but it works too, Torren_Mr 334 — 4y

1 answer

Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

I think the best way you would do this is with an algorithm, and the .Touched event.

-- inside a ServerScript inside a Part
local touches = {};
local cooldown = 5; -- The amount of time to wait between damaging

script.Parent.Touched:Connect(function(hit)
   local h = hit:FindFirstChildOfClass("Humanoid") or hit.Parent:FindFirstChildOfClass("Humanoid");
if (h) then -- check to see if whatever is hit has a humanoid
     local hitChar = h.Parent;
     if touches[hitChar] == true then return end; -- check if the model has already taken damage
     touches[hitChar] = true;
     h:TakeDamage(h.Health / 2); -- take damage in accordance to their Health. If they have 50 health, they'll take 25 damage. If they have 90 health, they'll take 45 damage. If they have 100 health, they'll take 50 damage.
spawn(function()
wait(cooldown);
touches[hitChar] = false or nil;
end)
   end
end)
Ad

Answer this question