Creating a way for the player to check their own health

I want to create a script that allows the player to press a key and it will check health and show different texts for different health, for example

Health at 15, “I need to stop”

Health at 80. “I’ll be alright”

I honestly just have no clue how to go about this, and would like any guidance in creating this, I have gone through 2 forums which I will link but I couldn’t figure out how to change them to what I need, I was just trying to figure out out to check the players health, but I don’t know much about coding!

local plr = game.Players.LocalPlayer
local Char = plr.CharacterAdded:Wait()
local Humanoid = Char:WaitForChild()
local UIS = game.UserInputService


UIS.InputBegan:Connect(function(Health, input)
	if input.Keycode == Enum.KeyCode.Q and (Health / Humanoid.MaxHealth * 100) <= 15 then
		print("I am at 15 health") else
	end
end)

and

local plr = game.Players.LocalPlayer
local Char = plr.CharacterAdded:Wait()
local Humanoid = Char:WaitForChild()
local UIS = game.UserInputService

local function DetectHealth(Health)
	if (Health / Humanoid.MaxHealth * 100) <= 15 then
		print("I am at 15 health")
	end
end

Humanoid:GetPropertyChangedSignal("Health"):Connect(DetectHealth())

How do I make when a player presses a keycode they receive a forcefield

Detect player health percentage - #5 by GetGlobals

2 Likes

Well its a good time to learn some tutorials then, but i will fix your mistake here. Im not fully sure if this is what you want but its all i can offer to help.

First script:

local plr = game:GetService("Players").LocalPlayer
local Char = plr.Character or plr.CharacterAdded:Wait()
local Humanoid = Char:WaitForChild("Humanoid")
local UIS = game:GetService("UserInputService")


UIS.InputBegan:Connect(function(input)
	if input.Keycode == Enum.KeyCode.Q and Humanoid.Health <= 15 then
		print("I am at 15 health") else
	elseif input.Keycode == Enum.KeyCode.Q and Humanoid.Health >= 80 then
		print(“I’ll be alright”)
	end
end)

Second Script:

local plr = game:GetService("Players").LocalPlayer
local Char = plr.Character or plr.CharacterAdded:Wait()
local Humanoid = Char:WaitForChild("Humanoid")
local UIS = game:GetService("UserInputService")

local function DetectHealth(Health)
	if Humanoid.Health <= 15 then
		print(“I need to stop”)
	elseif Humanoid.Health >= 80 then
		print(“I’ll be alright”)
	end
end

Humanoid:GetPropertyChangedSignal("Health"):Connect(DetectHealth)

Not perfect but it should be good enough i hope, let me know the results. Also please watch some tutorials they will help you a bunch! :slight_smile:

2 Likes

You can simply do the following:

local UserInputService = game:GetService("UserInputService")
local Players = game:GetService("Players")

local Player = Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")

UserInputService.InputBegan:Connect(function(input, _gameProcessed)
	if input.KeyCode == Enum.KeyCode.Q then
		local HumanoidHealth = Humanoid.Health
		if HumanoidHealth >= 80 then
			print("I'll be Alright")
		elseif HumanoidHealth <= 15 then
			print("I need to Stop")
		end
	end
end)
1 Like

It wasn’t working at first but it helped me a lot to figuring out how to make it work, thank you! Something to do with checking multiple different health levels was making the script break, and I have a lot more lines of code that I would like to figure out how to optimize and shorten.

But here is what was made so far, thank you again!

UIS.InputBegan:Connect(function(input, gpe)		
	if gpe then return end	
	
	if input.KeyCode == Enum.KeyCode.Q and Humanoid.Health == 100 and debounce == false then
		debounce = true
		print("I'm not hurt.")
		tweenMax()
		wait(2)
		debounce = false
	elseif input.KeyCode == Enum.KeyCode.Q and Humanoid.Health >= 80 and debounce == false then
		debounce = true
		print("Just a scratch.")
		tweenScratch()
		wait(2)
		debounce = false

2 Likes

Small advice:

You can move the “debounce == false” statement to the beginning.

Like this:

if gpe or debounce then return end

And instead of doing wait(2) on each elseif ... then statement, you can move it outside the loop. Like this:

if ... then

elseif ... then

end

task.wait(2) --// I use task.wait() instead, wait() is deprecated.
debounce = false

Just some small advice to improve readability and avoid duplicating lines/statements.

2 Likes

Very helpful! Would you have any idea on shortening the tweens? Right now I have them as functions above everything and I am just recalling them, but it seems like they are pretty long

local function tweenMax()
	local Max = MENU["Full Health"].TextLabel
	TweenService:Create(
		Max,
		TweenInfo.new(.5), 
		{TextTransparency = 0}
	):Play()
	wait(2)	
	TweenService:Create(
		Max,
		TweenInfo.new(.5), 
		{TextTransparency = 1} 
	):Play()
end

It is fine this way, sadly, it will always end up long when Tweening Properties that are not Size or Position, since you always have to use TweenService:Create(). Just the nature of how TweenService works.

1 Like

Alright well thank you for that tip, it helped a lot in shortening it and I will continue using it

1 Like

Glad to help. But i reccomend you watch some tutorials, they are more helpful than people let on.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.