Script not doing what it's told?

The script is adding 50 health to my character but when i press the button it adds 150 Health and Max Health. I did have it like this before, but I have since changed it just to add 50 health.
2. This is not on team create
1e9ed9b2f814e028e9eb38309507f8fc

local db = false
script.Parent.Touched:Connect(function(part)
	
	local humanoid
	 
	if part.Parent.Humanoid then
	humanoid = part.Parent.Humanoid
	elseif part.Parent.Parent.Humanoid then
	humanoid = part.Parent.Parent.Humanoid
	end
	
	if humanoid ~= nil and db == false then
		db = true
		if humanoid.Health >= 99 then
		humanoid.Health = humanoid.Health + 50
		script.Parent.Parent:Destroy()
		else if humanoid.Health == 100 then return end
		end
		wait(1)
		db = false
	end
end)
1 Like

Sometimes when you change it manually it does not do anything. Try changing it with a script in game.

1 Like

What do you mean by this?

30 characters

Over here you say that if the humanoid’s health is greater than or equal to 99, then add 50 health. Shouldn’t be less than or equal to 99?
image

good point, thanks for catching my typo.

When you manually changed the character’s health in Studio the script did not react to it and it did like if you had 100.

Okay, I’ll give this a try too.

This didn’t change anything really.

This worked but I don’t know why it’s still adding onto my max health. I mean I do, but like why is it still adding max health. I thought if you keep the max health at 100 the health can’t go over 100 hp?

I know it is strange but you can always write some code to fix the problem.

if humanoid.Health > humanoid.MaxHealth then
    humanoid.Health = 100 --or your MaxHealth
end

You are changing your health on the client in that GIF, which does not replicate to the server. The server still sees you have 100 health, not 2.

1 Like

Yeah, i just went to test this and it’s still not working. Kind of odd.

If it’s written on a server-sided script [a normal script] then the problem is that you are editing the
value locally.

Because when you change a value locally, the value doesn’t replicate over to server so the server will still see the value to be 100 [which I presume is full health.]

The next time you test your script out, try changing the value after you click “Current: Client” on the Top -middle section of your screen and check if it works.
Shown here in a gif:
https://gyazo.com/138ace88e983cdf504d371163710f417

yeah, vikenmanoukian21 solved this.

Feel free to mark his response as the solution so other members can quickly navigate to it in the future.

1 Like

Yeah, but I still can’t seem how to fix the issue on how to stop the issue of it still bringing the max health to 150 instead of it staying at 100.

If you don’t want the Health to overshoot MaxHealth, a simple math.min does the trick:

humanoid.health = math.min(humanoid.Health + 50, humanoid.MaxHealth)

math.min returns the minimum of its inputs.

This doesn’t seem to work either.

Can you explain what happens now?
Also please provide what your code looks like at the moment, since you probably changed it.

When i touch the part, The health goes up by 50 of course. Then I go to touch another (by now the health has regenerated enough so it’s going to be over 100 when i touch this)
The Health goes to whatever it is, and the MaxHealth goes to 150.