If statement not running

The “if statement” inside the proximity prompt function is not running the way I want.

local Part = script.Parent
local Prompt = Part.ProximityPrompt
--
Prompt.Triggered:Connect(function(player)
	--
	local Character = player.Character or player.CharacterAdded:Wait()
	local Humanoid = Character:FindFirstChildOfClass("Humanoid")
	--
	if Humanoid.Health < Humanoid.MaxHealth then
		--
		Humanoid.Health = Humanoid.Health + 4
		--
	else
		print("Already at MaxHealth")
	end
	--
end)

The problem is that when the Humanoid has less than MaxHealth it still prints “Already at MaxHealth”

Any help is appreciated!

Try to put elseif Humanoid.Health > Humanoid.MaxHealth then. This is testing to see if the Humanoid has less more health than max health, by using the greater than symbol. Let me know if it works or doesn’t.

I tried elseif Humanoid.Health > Humanoid.MaxHealth then & elseif Humanoid.Health >= Humanoid.MaxHealth then but nothing happened.

Maybe try to remove the or player.CharacterAdded:Wait() function. Just keep the player.Character. See if that works. You might be getting stuck on the wait function for CharacterAdded

Nope… still doesn’t seem to run. I mean it runs without errors, but even when the player is damaged and not at Max Health it still prints “Already at MaxHealth”

Try adding a print statement inside your if-statement and see if it prints in the output.

This is what I added:

Prompt.Triggered:Connect(function(player)
	--
	local Character = player.Character or player.CharacterAdded:Wait()
	local Humanoid = Character:FindFirstChild("Humanoid")
	--
	if Humanoid.Health < Humanoid.MaxHealth then
		--
		print("Before Adding 4")
		Humanoid.Health = Humanoid.Health + 4
		print("After Adding 4")
		--
	else
		print("Already at MaxHealth")
	end
	--
end)

And still only prints “Already at MaxHealth”

Try decreasing the humanoid’s health and see if the first if-statement works

That’s what I’ve been doing to test it out.

Lemme test this inside studio, brb

Although I don’t know how to fix this issue because there’s too little information provided, you don’t have to use if statements.

Image from Gyazo

Oh I see what your saying, let me give it a try.

Are you decreasing the humanoid’s health on the client? Because after decreasing the humanoid’s health on the server, the prompt seemed to work for me.

This is what I put

local Part = script.Parent
local Prompt = Part.ProximityPrompt
--
Prompt.Triggered:Connect(function(player)
	--
	local Character = player.Character or player.CharacterAdded:Wait()
	local Humanoid = Character:FindFirstChild("Humanoid")
	--
	Humanoid.Health = Humanoid.Health + 4
	--
end)

But for some reason it still doesn’t work.

What do you mean, like a Local Script or Server Script?

Because I’m using a Server Script for this.

Screen Shot 2022-11-21 at 3.33.27 PM

it’s probably because you are using a local script, as @Dpelayo5907 said.

The script you’re using to decrease the humanoid’s health. Are you using a LocalScript or a ServerScript?

I’m using a Server Script. (I sent screen shot in the other reply)

the reason this happens is (probably) because you are decreasing your health from the client. try switching to the ‘server’ view in the studio and decreasing your health.
if it’s not the reason, then sorry, but I don’t have any ideas left anymore.

1 Like

This is what I meant:

Decreasing the humanoid’s health on the client:
https://gyazo.com/fb2422ece2eefc61e3736b03e1b9f525

Decreasing the humanoid’s health on the server:
https://gyazo.com/3c321dffd3a6ff1ae3c56cd85ff934d0