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

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

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.

1 comment

0

When you run this in studio, what does the console say?

1 answer

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!

4 comments

0

Ok thank you! The formatting doesn't matter, it's actually better, as retyping it can help me learn what the code says.

0

Does it work? If so please put answered on your question

0

theres a scripting set up button u know right

0

Im aware but im not sure how to use it on mobile

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