Help With Making a Custom Health Bar Script Fixed

well, roblox studio thinks it is because on the side when im typing up the variable, it says it is a player. also when i type .team it auto comes up too.

It likely does this in Studio because it recognises the class GetPlayerFromCharacter() should return. You should still print whatever value script.Parent.Parent.Parent.Parent.Parent is, because it’s probably referencing Workspace, and since Workspace isn’t the character object of any players, the function will just return nil.

It is referring to the StarterPlayer instance.

During runtime, your script is cloned to the player’s character. This means the ancestry changes, and we’re no longer referring to StarterPlayer nor StarterCharacterScripts.

The full structure to the color changing script is this during runtime:
workspace.Player1.HealthScript.Health.Meter.ColorChange
Since the fourth parent is Player1, the character object, we can use GetPlayerFromCharacter(script.Parent.Parent.Parent.Parent) to get the player object.

Even if StarterPlayer was being referenced, GetPlayerFromCharacter() specifically wants a character object - the ingame avatar. StarterPlayer is just a service that sets the properties of the player and character objects, not at all referencing anyone’s player object nor the character object.

you are referring to the healthscript at this point.

Thanks to everyone who contributed! I have fixed the script, it works in a normal script for everyone who thought otherwise! Here is the completed 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 = 6355525

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)
}
print(player.Character.Name)

while wait() do
	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

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