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())
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!
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)
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
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.