Help With Making a Custom Health Bar Script Fixed

You can write your topic however you want, but you need to answer these questions:

  1. Hi there! I am trying to make a custom health bar for my game.

  2. For some reason, an error keeps being returned (see in bold below).

  3. I have looked everywhere, even followed a tutorial. Still no luck.

The script used to work, I don’t know whether there has been a recent Roblox Studio update or if Roblox is acting weird at the moment, but it was working 2 days ago for sure. The script is a normal script inside of StarterCharacterScripts and the player variable is located at StarterPlayer

SCRIPT

local Teams = game:GetService("Teams")
local TweenService = game:GetService("TweenService")

local player = game.Players:GetPlayerFromCharacter(script.Parent.Parent.Parent.Parent.Parent)
local meter = script.Parent

local groupId = ""

while wait() do
	local rainbowColors = {
		Color3.fromRGB(255, 0, 0), -- red
		Color3.fromRGB(255, 127, 0), -- orange
		Color3.fromRGB(255, 255, 0), -- yellow
		Color3.fromRGB(0, 255, 0), -- green
		Color3.fromRGB(0, 0, 255), -- blue
		Color3.fromRGB(148, 0, 211) -- purple
	}
	
	local orangeColors = {
		Color3.fromRGB(236, 172, 94),
		Color3.fromRGB(236, 120, 4),
		Color3.fromRGB(207, 108, 9),
		Color3.fromRGB(172, 92, 13),
		Color3.fromRGB(207, 108, 9),
		Color3.fromRGB(236, 120, 4)
	}
	
	local blueColors = {
		Color3.fromRGB(126, 174, 250),
		Color3.fromRGB(4, 175, 236),
		Color3.fromRGB(9, 137, 207),
		Color3.fromRGB(13, 105, 172),
		Color3.fromRGB(9, 137, 207),
		Color3.fromRGB(4, 175, 236)
	}
	
	if player.Team == Teams.Host then
		for i = 1, #rainbowColors do
			local nextColor = rainbowColors[i]
			local tweenInfo = TweenInfo.new(0.5) -- adjust duration as needed

			local tween = TweenService:Create(meter, tweenInfo, {
				BackgroundColor3 = nextColor
			})

			tween:Play()
			tween.Completed:Wait()
		end
	elseif player.Team == Teams["Lobby Duty"] then
		for i = 1, #blueColors do
			local nextColor = blueColors[i]
			local tweenInfo = TweenInfo.new(0.5) -- adjust duration as needed

			local tween = TweenService:Create(meter, tweenInfo, {
				BackgroundColor3 = nextColor
			})

			tween:Play()
			tween.Completed:Wait()
		end
	else
		meter.BackgroundColor3 = Color3.fromRGB(0, 255, 0)
	end
end

ERROR

  08:53:42.215  Workspace.greeninja_97.HealthScript.Health.Meter.ColorChange:83: attempt to index nil with 'Team'  -  Server - ColorChange:83
  08:53:42.216  Script 'Workspace.greeninja_97.HealthScript.Health.Meter.ColorChange', Line 83  -  Studio - ColorChange:83
1 Like

What exactly is script.Parent.Parent.Parent.Parent.Parent? If it is trying to index nil with Team, maybe the Player is not being detected? Could you provide a screenshot of the ancestry of this script?

That is referring to the StarterPlayer instance (which should refer to the player when they join the game - it says it is a player when I refer to the variable).

Do you have a screenshot of where the script is located in the Explorer?

Why are you refering to the player? Getplayerfromcharacter uses the character…

1 Like

True - if script.Parent.Parent.Parent.Parent.Parent is the player then just set the player variable to script.Parent.Parent.Parent.Parent.Parent (this is such a mouthful lol).

Also, you don’t need to define all the color tables every second, since they never change, it is a waste.

1 Like

True, thanks for spotting that. Yes it is a constant table.

Sure, here:

image

Like I said before, it worked for a while, now it isn’t.

Why not use a localscript? Then you can get the player a lot easier.

Because, I want the healthbar colors to appear the exact same for every player in the server, so that one player doesn’t see a different point in the color sequence. I want the healthtag to appear the same for everyone at the same time.

Since players will join at different times, it will ultimately be different because the script will start at different times… If you want it to be synchronized, you can use a shared module to tell the script what color to use.

Maybe instead of game.Players:GetPlayerFromCharacter(script.Parent.Parent.Parent.Parent.Parent) you could’ve simply used game:GetService("Players").LocalPlayer?

…Also, who uses normal scripts for UI’s? If anything, it only results in laggy UI movement and stuff.

Like what @sorpoy said, it is better to do healthbars and stuff the server doesn’t need on the client

Okay, I have had no experience with modules, all I know is that the script worked before. Has there been a recent Roblox Studio update that has altered any changes that could have made my script work before?

Because, as I said before it is a normal script (and I wish it to remain the same).

I don’t think it was clarified if this healthbar appears above the player’s head, or is it purely part of the player’s UI? In the case of the latter, there is no need to sync healthbar colours, so I would suggest copying all the code from that ColorChange script into a LocalScript and just changing the player variable to game.Players.LocalPlayer. In the case of the former, I’m not sure what a “shared module” can be defined as, but I would probably suggest what @hbgnhu said.

The character object should be at script.Parent.Parent.Parent.Parent instead of script.Parent.Parent.Parent.Parent.Parent. Use print() to verify that.

1 Like

Well, i want all the players in the game to see that, thats the point of the sequence of colors, to tell who the special ranks are and what not.