Only one line of code is being used

Why doesn’t this work? I am not getting any errors and honestly am just kinda stuck, an explanation would be helpful so that I can learn from it as well.

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
local percentage = math.floor((plr.Character.Humanoid.Health / plr.Character.Humanoid.MaxHealth) * 100)

UIS.InputBegan:Connect(function(input, gpe)		
	if gpe or debounce then return end	
	
	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()
	elseif input.KeyCode == Enum.KeyCode.Q and percentage >= 60 then
		debounce = true
		print("I'll be alright.")
		AnimationEvent:FireServer()
		tweenHalf()
	elseif input.KeyCode == Enum.KeyCode.Q and percentage >= 30 then
		debounce = true
		print("I should be careful!")
		AnimationEvent:FireServer()
		tweenQuarter()
	elseif input.KeyCode == Enum.KeyCode.Q and percentage >= 0 then
		debounce = true
		print("I need to get to safety!")
		AnimationEvent:FireServer()
		tweenDanger()
	end
	task.wait(3)
	debounce = false
end)

The code at the very bottom only ever shows “I’m not hurt” (the first line) no matter how low the health is

You aren’t defining this variable inside of the connected function (it will never be updated), so it will always be what it was when you first defined it. Simply move it into the function you’ve connected to UIS.InputBegan and it should work.

Also, on a side note, since you’re setting debounce to true and firing AnimationEvent in every if statement in that function, for readability purposes you may want to just move it outside of the ifs.

1 Like

You don’t have to check the same condition over and over again, just say

if (not input.KeyCode == Enum.KeyCode.Q) then return end

and then check the percentage, as a side note.

1 Like

In addition to what @Den_vers said, you can condense all those functions into a single function by doing this:

local function tween(name: string)
    local element = MENU:FindFirstChild(name).TextLabel -- send the name of the element to the function
    -- This gets rid of repeat functions that basically do the same thing
	TweenService:Create(
		element,
		TweenInfo.new(.5), 
		{TextTransparency = 0}
	):Play()
	wait(2)	
	TweenService:Create(
		element,
		TweenInfo.new(.5), 
		{TextTransparency = 1} 
	):Play()
end

-- Example usage:
tween("Danger") -- Tweens the danger element
1 Like

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