I'm trying to make a GUI button that makes your character run, and there is an energy bar that slowly goes down while you're running. what's wrong with this script?

local FRAME = script.Parent
local ENERGY_VALUE = FRAME.EnergyLeft
local MAX_ENERGY = ENERGY_VALUE.Value
local ENERGY_BAR = FRAME.EnergyFrame.EnergyBar
local BUTTON = FRAME.RunButton

local RUNNING = false
local DEBOUNCE = true

local WS = game.Players.localPlayer.Character.Humanoid.WalkSpeed 

function getPC()
	local player = FRAME.Parent.Parent.Parent
	local char = player.Character

	return player, char
end

function getEnergy()
	return ENERGY_VALUE.Value
end

function modEnergy(val)
	ENERGY_VALUE.Value = ENERGY_VALUE.Value + val

	if getEnergy() >= MAX_ENERGY then
		ENERGY_VALUE.Value = MAX_ENERGY
	end

	if getEnergy() <= 0 then
		ENERGY_VALUE.Value = 0
	end
end

function switchRunningState()
	local player, char = getPC()

	if RUNNING then
		WS = 135

		RUNNING = true
	else
		WS = 16

		RUNNING = true
	end
end

function onMouseButton1Down()
	if not DEBOUNCE then
		return
	end

	if RUNNING then
		switchRunningState()

	else
		if getEnergy() > 10 then
			switchRunningState()
		end
	end

	DEBOUNCE = false
	wait(2)
	DEBOUNCE = true
end

function setVisibleState()
	local player, char = getPC()
	local torso = char.Torso

		BUTTON.Visible = true
	end

function updateGUI()
	ENERGY_BAR.Size = UDim2.new(getEnergy() / MAX_ENERGY, 0, 1, 0)

	if getEnergy() == MAX_ENERGY then
		ENERGY_BAR.Parent.Visible = false
	else
		ENERGY_BAR.Parent.Visible = true
	end
end

function mainLoop()
	while true do
		if RUNNING then
			modEnergy(-0.1)

			if getEnergy() <= 0 then
				switchRunningState()
			end
		else
			modEnergy(0.1)
		end

		setVisibleState()
		updateGUI()

		wait(0.1)
	end
end

BUTTON.MouseButton1Down:connect(onMouseButton1Down)

mainLoop()

4 comments

0

You're giving us a 106 lined script and telling us to fix it? Not constructive.

0

i'm not asking for it to be fixed, i'm asking what is making it not work

0

in other words asking it to be fixed because you don't know how to look at the output and can't be bothered telling us what's happening and what's not working.

0

Sometimes there are no errors in the output...