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

Health script help?

If the hum.Health is set to "0", it works, if its set to 50, it does not take away half health.

function Touched(hit) --"Touched" is name of the function.
	
	local hum = hit.Parent:findFirstChild("Humanoid")
	
	if hum then
		hum.Health = 50	
		
	end
end

script.Parent.Touched:connect(Touched)

5 comments

0

Did you check the 'Health' Value of the Player's 'Humanoid' in Play-Solo mode?

0

nope, just change the script up

0

What do you mean by 'just change the script up'?

0

HELP ME??!!! LOL

0

I don't understand what you are trying to say; You're very vague on your Comments.

2 answers

✓ Accepted answer

You're not removing half the players health, you're just setting it to 50.

function Touched(hit) --"Touched" is name of the function.

    local hum = hit.Parent:findFirstChild("Humanoid")

    if hum then
        hum.Health = hum.Health - (hum.Health/2) --subtracts half the health, this won't ever kill the player though

    end
end

script.Parent.Touched:connect(Touched)

If you want it to take away half health, i suggest introducing the "MaxHealth" property which will return the player's max health (obviously) and then dividing it by 2.

For example:

function Touch(hit)
	local hum = hit.Parent:FindFirstChild'Humanoid'
	if hum then
		hum:TakeDamage(hum.MaxHealth / 2) -- take away half health
		-- (Or you could say hum.Health / 2, however this would just keep dividing the player's current health by 2, and wouldn't actually kill the player. Would just keep lowering it's health to basically anything but 0 (like YellowTide mentioned))
	end
end

script.Parent.Touched:connect(Touch)

However, this will still instantly kill the player. What you also need is something called a debounce method.

Explaining events

Events all work in a very common way. They will execute code when the event fires, without it waiting for the code to finish. This is where debounce comes in.

Debounce

Debounce is nothing more than making the script not run code until it's statement is true again. While it's statement is false (or nil), the event will not run the code in the event. This only happens if we use an if statement, while using a debounce method.

Exmaple:

local TouchEnabled = true  -- the debounce method
local Object = script.Parent

function Touch(Hit)
	local hum = Hit.Parent:FindFirstChild'Humanoid'
	if hum and TouchEnabled then -- TouchEnabled must be true for this to run

		TouchEnabled = false -- it's false, so this event will not run twice until the code under it is finished

		hum:TakeDamage(hum.MaxHealth/2)

		wait(1) -- wait 1 second before enabling out event again, so it doesn't just instantly take away half the player's health like 10 times in 1 touch

		TouchEnabled = true -- enable it again
	end
end

Hope this helped. Let me know if you have any questions.

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