Script Not Working

Hello, I am making a Overhead Gui is supposed to display the players Rank, from a leaderstat, but it doesn’t change the text.
The Script:

local billboardgui = game:GetService("ServerStorage"):WaitForChild("NameTag")

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		if player.Team == game.Teams.Prisoners then
			local Clone = billboardgui:Clone()
			Clone.Name = player.UserId
		Clone.Title.Text = player.Name
		Clone.Title.TextColor3 = Color3.fromRGB(255, 128, 0)
		Clone.Icon.Rank.Text = player:WaitForChild("leaderstats"):WaitForChild("Rank").Value
		Clone.Parent = game.Workspace:WaitForChild(player.Name).UpperTorso
		end
		if player.Team == game.Teams.Police then
			local Clone = billboardgui:Clone()
			Clone.Name = player.UserId
			Clone.Title.Text = player.Name
			Clone.Title.TextColor3 = Color3.fromRGB(85, 170, 255)
			Clone.Icon.Rank.Text = player:WaitForChild("leaderstats"):WaitForChild("Rank").Value
			Clone.Parent = game.Workspace:WaitForChild(player.Name).UpperTorso
		end
		if player.Team == game.Teams.Criminals then
			local Clone = billboardgui:Clone()
			Clone.Name = player.UserId
			Clone.Title.Text = player.Name
			Clone.Title.TextColor3 = Color3.fromRGB(170, 0, 0)
			Clone.Icon.Rank.Text = player:WaitForChild("leaderstats"):WaitForChild("Rank").Value
			Clone.Parent = game.Workspace:WaitForChild(player.Name).UpperTorso
		end
		if player.Team == game.Teams.Heroes then
			local Clone = billboardgui:Clone()
			Clone.Name = player.UserId
			Clone.Title.Text = player.Name
			Clone.Title.TextColor3 = Color3.fromRGB(170, 170, 0)
			Clone.Icon.Rank.Text = player:WaitForChild("leaderstats"):WaitForChild("Rank").Value
			Clone.Parent = game.Workspace:WaitForChild(player.Name).UpperTorso
		end
	
	end)
end)

Were i change the Text when the player Ranks up (Also Doesn’t work)

local Player = game.Players:GetPlayerByUserId(script.Parent.Name)
Player:WaitForChild("leaderstats"):WaitForChild("Rank").Value.Changed:Connect(function(new)
	script.Parent.Title.Text = new
end)
Player.Team.Changed:Connect(function(newTeam)
	if newTeam == game.Teams.Prisoners then
		script.Parent.Title.TextColor3 = Color3.fromRGB(170, 85, 0)
	end
	if newTeam == game.Teams.Police then
		script.Parent.Title.TextColor3 = Color3.fromRGB(85, 170, 255)
	end
	if newTeam == game.Teams.Prisoners then
		script.Parent.Title.TextColor3 = Color3.fromRGB(255, 0, 0)
	end
	if newTeam == game.Teams.Prisoners then
		script.Parent.Title.TextColor3 = Color3.fromRGB(255, 170, 0)
	end
end)

1 Like

Are you sure the player’s are assigned to that team when they join and not Neutral.

1 Like

They are assigned to a team, that’s not the problem, its this code here:

Clone.Icon.Rank.Text = player:WaitForChild("leaderstats"):WaitForChild("Rank").Value
1 Like
Clone.Icon.Rank.Text = player:WaitForChild("leaderstats"):FindFirstChild("Rank").Value

You shouldn’t be using :WaitForChild for something more than something added directly to the player. In fact you can probably replace both of them with FindFirstChild because I assume that leaderstats will already exist once you are trying to call upon it therefore it is going to infinitely yield. Just a recommendation don’t know if this will solve your issue.

1 Like

That didn’t work, i didn’t get any errors either

1 Like

One of the main issues I am seeing here is like what I said, You are using wayyy too many :WaitForChild() instead of :FindFirstChild()… Another thing is… Is this a Server Script or a LocalScript?

1 Like

You are checking the same thing 3 times here aswell.

1 Like

Its a server script, and i changed it to find first child, it didn’t work

1 Like

Why are you checking game.Players.Team.Prisoners 3 times

1 Like

That was a accident, it was meant to be this:

Player.Team.Changed:Connect(function(newTeam)
	if newTeam == game.Teams.Prisoners then
		script.Parent.Title.TextColor3 = Color3.fromRGB(170, 85, 0)
	end
	if newTeam == game.Teams.Police then
		script.Parent.Title.TextColor3 = Color3.fromRGB(85, 170, 255)
	end
	if newTeam == game.Teams.Criminals then
		script.Parent.Title.TextColor3 = Color3.fromRGB(255, 0, 0)
	end
	if newTeam == game.Teams.Civilians then
		script.Parent.Title.TextColor3 = Color3.fromRGB(255, 170, 0)
	end
1 Like

Does it work when you do that…?

1 Like

Sadly no, it just says the default text

1 Like

Also instead of having 4 seperate ifs use an elseif

if this then

elseif that then

elseif thisthat then

elseif doSomething then

else
--// It was none of the above, return
return;
end
1 Like
Player.Team.Changed:Connect(function(newTeam)
	if newTeam == game.Teams.Prisoners then
		script.Parent.Title.TextColor3 = Color3.fromRGB(170, 85, 0)
	elseif newTeam == game.Teams.Police then
		script.Parent.Title.TextColor3 = Color3.fromRGB(85, 170, 255)
	elseif newTeam == game.Teams.Criminals then
		script.Parent.Title.TextColor3 = Color3.fromRGB(255, 0, 0)
	elseif newTeam == game.Teams.Civilians then
		script.Parent.Title.TextColor3 = Color3.fromRGB(255, 170, 0)
	end
end)
1 Like