FuelBarUI Not Re-appearing upon Spawn

local player = game.Players.LocalPlayer
local fuelBar = player.PlayerGui:WaitForChild("FuelBar")
local fullBar = fuelBar:WaitForChild("ImageFrame"):WaitForChild("Empty"):WaitForChild("Full")

local lantern = game.Workspace:WaitForChild(player.Name):WaitForChild("Lantern")
local fuel = lantern:WaitForChild("Fuel")

local function updateFuelBarUI()
	local fuelRatio = fuel.Value / 75
	fuelRatio = math.clamp(fuelRatio, 0, 1)

	fullBar.AnchorPoint = Vector2.new(0.5, 0.5)
	fullBar.Size = UDim2.new(fuelRatio * 0.972, 0, 0.75, 0)
	fullBar.Position = UDim2.new(0.5, 0, 0.5, 0)

	if fuel.Value == 0 then
		fullBar.BackgroundColor3 = Color3.fromRGB(255, 0, 0)
	else
		fullBar.BackgroundColor3 = Color3.fromRGB(0, 255, 0)
	end
end

local function toggleFuelBarVisibility(isLanternEquipped)
	if isLanternEquipped then
		fuelBar.Enabled = true
	else
		fuelBar.Enabled = false
	end
end

spawn(function()
	while true do
		wait(0.1)
		local isLanternEquipped = lantern.Parent == player.Character
		toggleFuelBarVisibility(isLanternEquipped)

		if isLanternEquipped then
			updateFuelBarUI()
		end
	end
end)

game:GetService("ReplicatedStorage").UpdateFuel.OnClientEvent:Connect(function(newFuelValue)
	fuel.Value = newFuelValue
	updateFuelBarUI()
end)

This is my current code portraying within the video seen below. I just can’t seem to get the UI to reload upon respawn for when the lantern is equipped!

Any Support?

https://gyazo.com/6376a84e71ef6ff55da4e08a183f41f0

There is a ResetOnSpawn check box in the Property panel.

Maybe that will help:

Screen Shot 2024-11-21 at 4.34.09 PM

Yeah, This is enabled.

image

Check the PlayerGUI to make sure your GUI it is there on Respawn.

Screen Shot 2024-11-21 at 4.38.38 PM

image
Yeah It shows it’s still here upon respawn but doesn’t allow enabled when the lantern is equipped again?

Where is the script shown above located?

It’s a LocalScript located within StarterPlayerScripts

You have the variables for the GUI stated at the top of the script.

They will not be valid if the GUI was replaced.

You need to define them within the function, that way they are refreshed with the GUI.

1 Like

It works now, I appreciate the support!

1 Like