Converting to percentages, and optimization

How can I change all my if number is equal to etc into percentages so that players who have health upgrades etc it wont break, also I am pretty sure I don’t have to have all of those keycode lines but I don’t know how to go about that

Health check that I need to be converted is at the very bottom

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 AnimationEvent = game.ReplicatedStorage.AnimationEvent

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.")
		AnimationEvent:FireServer()
		tweenMax()
	elseif input.KeyCode == Enum.KeyCode.Q and Humanoid.Health >= 80 then
		debounce = true
		print("Just a scratch.")
		AnimationEvent:FireServer()
		tweenScratch()
	elseif input.KeyCode == Enum.KeyCode.Q and Humanoid.Health >= 60 then
		debounce = true
		print("I'll be alright.")
		AnimationEvent:FireServer()
		tweenHalf()
	elseif input.KeyCode == Enum.KeyCode.Q and Humanoid.Health >= 30 then
		debounce = true
		print("I should be careful!")
		AnimationEvent:FireServer()
		tweenQuarter()
	elseif input.KeyCode == Enum.KeyCode.Q and Humanoid.Health >= 0 then
		debounce = true
		print("I need to get to safety!")
		AnimationEvent:FireServer()
		tweenDanger()
	end
	task.wait(3)
	debounce = false
end)

To convert a number to a percentage, do the following maths:

current value divided by total value, multiplied by 100

Then, you can use the math.floor() to take the whole number.
This would look like:

local percentage = math.floor((player.Character.Humanoid.Health / player.Character.Humanoid.MaxHealth) * 100)
1 Like

How can I put this into my script?

if input.KeyCode == Enum.KeyCode.Q and percentage == 100 then
		debounce = true
		print("I'm not hurt.")
		AnimationEvent:FireServer()
		tweenMax()
elseif input.KeyCode == Enum.KeyCode.Q and percentage >= 80 then
		debounce = true
		print("Just a scratch.")
		AnimationEvent:FireServer()
		tweenScratch()

I’ve changed it to this for every line, whole code not included, but now it will only ever say “I’m not hurt”

Add a print() statement after it just to check if the output is working correctly.

Also, the >= 80 part will also account for 100 percent. Try adding a <= 99.