Humanoid.Health < 30 doesnt work

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    I want to make the script do something once the humanoid.health is under 30

  2. What is the issue? Include screenshots / videos if possible!
    basically I have a humanoid.healthchanged in a local script then I fire a remote event, and on the server I want to add an revive button once someone goes under 30 health, but what I did to check was this:

if humanoid.Health < 30 then
print("health under 30")
--some more code
if humanoid.Health >= 30 then
print("Health = 30+")
--some more code

Even tho my health is 15 it keeps printing “Health = 30+”. I can add more code if needed and a quick note is that the player has 130 health. Please help!

2 Likes

Are you using any sort of events/ loops to run this code? If not, the code will only run once with the health prior to it being updated.

1 Like

This is the local script

local rep = game:GetService("ReplicatedStorage")
local player = game.Players.LocalPlayer
local character = player.Character
local rem = rep.Events:WaitForChild("Revive")
local humanoid = character:FindFirstChildWhichIsA("Humanoid")
humanoid.HealthChanged:Connect(function()
	if humanoid.Health < 30 then
		game.StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack, false)
		rem:FireServer()
		print("Fired to server")
		wait(0.1)
		local revbutton = character.Torso.ReviveGui.RevButton
		revbutton.Visible = false
	end
	if humanoid.Health >= 30 then
		game.StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack, true)
		rem:FireServer()
	end	
end)

This is the server script

local rep = game:GetService("ReplicatedStorage")
local remote = rep.Events.Revive
remote.OnServerEvent:Connect(function(player)
	print("Fired On Server")
	local character = player.Character
	local humanoid = character:FindFirstChildWhichIsA("Humanoid")
	if humanoid.Health < 30 then
		print("health under 30")
		humanoid.WalkSpeed = 5
		print("Trying animations")
		character.Animate.walk.WalkAnim.AnimationId = "rbxassetid://7229426734"
		character.Animate.run.RunAnim.AnimationId = "rbxassetid://7229426734"
		character.Animate.idle.Animation1.AnimationId = "rbxassetid://7229422044"
		print("Done animations")
		local revivegui = rep.BillboardGuis:WaitForChild("ReviveGui"):Clone()
		revivegui.Parent = character.Torso
		local revivebutton = revivegui.RevButton
		revivebutton.Text = "Revive"
		revivebutton.MouseButton1Click:Connect(function()
			wait(3)
			revivebutton.Text = "Reviving"
			humanoid.Health = 30
			revivebutton:Destroy()
		end)
	end
	if humanoid.Health >= 30 then
		print("Health = 30+")
		local revivegui = character:FindFirstChild("ReviveGui")
		if revivegui then
			revivegui:Destroy()
		end
		humanoid.WalkSpeed = 16
		character.Animate.walk.WalkAnim.AnimationId = "rbxassetid://7228751837"
		character.Animate.run.RunAnim.AnimationId = "rbxassetid://7228750509"
		character.Animate.idle.Animation1.AnimationId = "rbxassetid://0"
	end
end)
1 Like

so a remote event to fire it every time the health changes

1 Like

You can do it like that:

if humanoid.Health < 30 then
    print('low health')
    --some more code
else
    print('great health')
    --some more code
1 Like

oh yeah thanks i will try that

1 Like

image
image
Still doesnt work :frowning:

1 Like

I dont want to use a loop. Its get fired everytime the health changes so no point in using a loop

1 Like

No don’t try that it will crash game

1 Like

and u didnt add a wait btw!!!

1 Like

crash the gam- why would that crash the whole game? have you even tested it out?

1 Like

Maybe because you didn’t add a wait() function??

1 Like

Okay I tried printing and it looks like humanoid.Health is actually the maxhealth??

1 Like

I hate to tell you but that is some nightmare fuel for code and honestly a really bad response. Firstly, you’re indefinitely running this script with no timeout, which will exhaust the script. Secondly you’re polling when there’s an Event which does exactly that but better, which is what they’re using.

Lastly, instead of using elseifs you just started another if statement, and didn’t even provide it an end so luckily this script wont break the game since it wont run in the first place due to the syntax error.

2 Likes

HealthChanged provides a parameter which is the new health, so just instead of doing humanoid.Health store the parameter as something like humHealth and then do if humHealth > val then.

1 Like

Alright I will try, i’ll keep u guys updated

1 Like

Wait, do you just enter the game and change your health as client??? If yes then that’s the reason why it isn’t working

1 Like

Try local character = player.Character or player.CharacterAdded:Wait()

1 Like

no no I have a seperate script to make the clients hp 130 when joining (in server script service) I use a local script only to make the hotbar invisible and send a remote

1 Like

Thanks this worked!!! Thanks everyone else for helping out!!!

1 Like