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

Is there a way to write a script that kills you if you're above your MaxHealth, etc. ?

Asked by 5 years ago
Edited 3 years ago

Okay, so a backstory:

I've whipped up a tool that heals you, but one problem is that the healing doesn't stop at the Max Health, and just keeps on healing your health above the Max Health, which is a problem as you could turn yourself into a monster with 1000+ health, when you're only supposed to have 100. Instead of fixing this, I decided it would be neat to turn it into a feature and make it to if you "overheal" yourself you can poison yourself and die.

So now I created a LocalScript that would quickly drain your health once you're 50 hp above your Max Health (I'm EXTREMELY NEW to scripting, and this is my first try at creating a script):

local Humanoid = game.Players.LocalPlayer.Character.Humanoid



function Kill()
    if Humanoid.Health > Humanoid.MaxHealth+50 then
        repeat
            Humanoid:TakeDamage(30)
            wait(1)
        until Humanoid.Died
    end
end

Kill()

I know that I wrote a script that's entirely wrong (I've tested it) and I'm hoping if I could find what's wrong with it, and find out how I can fix it.

It would also help if you can show me what's wrong with the script to help me for future scripts I may write.

EDIT:

Ok 6 month later, me reading this again I've seen the problem. Old me forgot to loop it.

EDIT 2:

Ok pogger me like 1.5 years later wants to say hi.

0
When you run this in studio, what does the console say? spot6003 64 — 5y

1 answer

Log in to vote
0
Answered by 5 years ago

Good job on setting up a variable for the player's humanoid object. From here you can use the Humanoid's HealthChanged event to detect whenever the health of the player changes like so:

local humanoid = game.Players.LocalPlayer.Character.Humanoid local currentHealth = humanoid.Health

humanoid.HealthChanged:Connect(function() if currentHealth > 150 then repeat humanoid.TakeDamage(30) until humanoid:Died() end end) end) Please excuse the formatting errors, this was written on phone, but i hope this helps!

0
Ok thank you! The formatting doesn't matter, it's actually better, as retyping it can help me learn what the code says. xMicro_Canary 37 — 5y
0
Does it work? If so please put answered on your question spot6003 64 — 5y
0
theres a scripting set up button u know right RobloxGameingStudios 145 — 5y
0
Im aware but im not sure how to use it on mobile spot6003 64 — 5y
Ad

Answer this question