First little project, health checking system

It checks the players health and gives different texts for different levels, it is meant to replace the health bar completely to make the game more challenging, I have glitched it to where I could keep walking after checking my health a few times, and I would like to add an animation but studio is down for me right now.

Any optimization or making the script more readable etc would be nice advice as well, I feel like there is something to get rid of all those tweens but I am not sure.

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 GUI = game.StarterGui
local MENU = game.Players.LocalPlayer.PlayerGui
local TweenService = game:GetService('TweenService')


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

local function tweenScratch()
	local Scratch = MENU.Scratch.TextLabel
	TweenService:Create(
		Scratch,
		TweenInfo.new(.5), 
		{TextTransparency = 0}
	):Play()
	wait(2)	
	TweenService:Create(
		Scratch,
		TweenInfo.new(.5), 
		{TextTransparency = 1} 
	):Play()
end

local function tweenHalf()
	local Half = MENU.Half.TextLabel
	TweenService:Create(
		Half,
		TweenInfo.new(.5), 
		{TextTransparency = 0}
	):Play()
	wait(2)	
	TweenService:Create(
		Half,
		TweenInfo.new(.5), 
		{TextTransparency = 1} 
	):Play()
end

local function tweenQuarter()
	local Quarter = MENU.Quarter.TextLabel
	TweenService:Create(
		Quarter,
		TweenInfo.new(.5), 
		{TextTransparency = 0}
	):Play()
	wait(2)	
	TweenService:Create(
		Quarter,
		TweenInfo.new(.5), 
		{TextTransparency = 1} 
	):Play()
end

local function tweenDanger()
	local Danger = MENU.Danger.TextLabel
	TweenService:Create(
		Danger,
		TweenInfo.new(.5), 
		{TextTransparency = 0}
	):Play()
	wait(2)	
	TweenService:Create(
		Danger,
		TweenInfo.new(.5), 
		{TextTransparency = 1} 
	):Play()
end

local debounce = false

UIS.InputBegan:Connect(function(input, gpe)		
	if gpe or debounce then return end	
	
	if input.KeyCode == Enum.KeyCode.Q and Humanoid.Health == 100 then
		debounce = true
		print("I'm not hurt.")
		Humanoid.WalkSpeed = 0
		tweenMax()
	elseif input.KeyCode == Enum.KeyCode.Q and Humanoid.Health >= 80 then
		debounce = true
		print("Just a scratch.")
		Humanoid.WalkSpeed = 0
		tweenScratch()
	elseif input.KeyCode == Enum.KeyCode.Q and Humanoid.Health >= 60 then
		debounce = true
		print("I'll be alright.")
		Humanoid.WalkSpeed = 0
		tweenHalf()
	elseif input.KeyCode == Enum.KeyCode.Q and Humanoid.Health >= 30 then
		debounce = true
		print("I should be careful!")
		Humanoid.WalkSpeed = 0
		tweenQuarter()
	elseif input.KeyCode == Enum.KeyCode.Q and Humanoid.Health >= 0 then
		debounce = true
		print("I need to get to safety!")
		Humanoid.WalkSpeed = 0
		tweenDanger()
	end
	task.wait(3)
	Humanoid.WalkSpeed = 16
	debounce = false
end)
2 Likes

Suggestion, too many if statements for input checks

-- Personal Suggestion
local UserInputService = game:GetService("UserInputService")
-- Avoiding using shortcut names for variables to increase readibility.
if input.KeyCode == Enum.KeyCode.Q then
 --code
end

is fine.

as for health you can store health messages in a table structured as follows

local HealthTips = {
   [100] = {
     Tween = TweenFull
     Message = "Message"
   }, -- [Index being health], and value the message
}

UserInputService.InputBegan:Connect(function(Input, Writing)
    if Writing or Debounce then return end
    if Input.KeyCode == Enum.KeyCode.Q then
          for Health, Data in pairs(HealthTips)
                 if Humanoid.Health >= Health then
                       Debounce = true
                       Data.Tween()
                       print(Data.Message)
		               Humanoid.WalkSpeed = 0
                       task.spawn(function()
                             task.wait(4) -- change number for different cooldown time.
                             Debounce = false
                       end)
                       return
                 end
          end
    end
end)
1 Like

I wish people followed this more often

The person writing the abbreviation has context about that code that the next person reading the code may not have.

2 Likes

Someone already suggested what I was saying

1 Like