Checking Health of player Doesn't works

I wanted to check If Player’s Health Is 0, If so then Dont trigger, if not Then trigger, but it doesnt seems to work

Script :

task.wait()
local UIS = game:GetService('UserInputService')
local Player = game.Players.LocalPlayer
local Character = Player.Character
local cooldown = false

local hum = Character:FindFirstChildWhichIsA("Humanoid")

local replicated = game:GetService("ReplicatedStorage")

local remote = replicated:WaitForChild("UseFlashlight")

local Key = "F"


UIS.InputBegan:Connect(function(key, chatted)
	if chatted then
		return
	end

	if key.KeyCode == Enum.KeyCode[Key] then
		print(hum.Health)
		if not cooldown and Character and not hum.Health <= 0 then
			remote:FireServer(key.KeyCode)
		end
	end
end)

Output :

Just do and hum.Health > 0 then

But I need that if it is 0 or less it doesn’t work

just use a opposite statement if you compare boolean to boolean

Ya if hum.Health > 0 Checks if it greater then 0 else it won’t work

But I want it to be activated if it is equal to 0 or less

Also you can’t just do If not cooldown you have to do If cooldown == false

hum.Health <1

sdiganhdngo

Then just do hum.Heath <= 0, None the less you still have to fix you’re cooldown statment

Yeah but still doesnt works, i tried hum.health < 1 and it didnt worked too, same output

Have you changed if not cooldown yet?
If not cooldown will only work if cooldown == nil

I didnt , so i decided to put it down

if key.KeyCode == Enum.KeyCode[Key] then
		print(hum.Health)
		if not cooldown and Character then
			if hum.Health >= 0 then
				remote:FireServer(key.KeyCode)
			end
		end
	end
end)

Change it to

if key.KeyCode == Enum.KeyCode[Key] then
		print(hum.Health)
		if cooldown == false and Character then
			if hum.Health <= 0 then
				remote:FireServer(key.KeyCode)
			end
		end
	end
end)

Doesn’t work properly as now I need to be dead for the remote to work

that’s what you said you wanted

I’m using a translator, sorry for the spelling errors

  1. Cooldown variable doesn’t exist.
  2. Just check if health is above 0 instead of not less than 0

Cooldown variable exist, you can see the full code above, also thats what i am doing

You’re confusing everyone, not gonna lie.
At first, you said:

Implying that if you’re dead, it shouldn’t work. Then you said the opposite:

which now implies you should be dead for the flashlight to activate.

Anyways, I tried fixing your code too. If it doesn’t work now… Well. I can’t really tell you much but good luck solving your issue.

UIS.InputBegan:Connect(function(key, chatted)
	if chatted then
		return
	end
	if key.KeyCode == Enum.KeyCode[Key] then
        if hum.Health <= 0 then
            warn("Humanoid is dead. Returning...")
        end
		if cooldown == false and Character ~= nil and hum.Health > 0 then
            print("Successfully fired the remote!")
			remote:FireServer(key.KeyCode)
		end
	end
end)
1 Like