Issue where when there is 2 or more players the script stops working

I am having an issue with a script that I’ve created that shows the player’s display name, username, and level. Whenever there is 1 player in-game the overhead display/GUI shows and works perfectly fine but when 2 or more players or in-game it breaks.

The Script:

local OverheadGui = script.OverheadGui:Clone()

local function updateOverheadGui(player)
	local character = player.Character
	if character then
		local leaderstats = player:WaitForChild("leaderstats")
		local level = leaderstats:WaitForChild("Level")

		OverheadGui.Username.Text = player.DisplayName .. " (@" .. player.Name .. ")"
		OverheadGui.Level.Text = "Level: " .. level.Value
		OverheadGui.Parent = character:WaitForChild("HumanoidRootPart")

		level.Changed:Connect(function(newValue)
			OverheadGui.Level.Text = "Level: " .. newValue
		end)
	end
end

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function()
		updateOverheadGui(player)
	end)
	updateOverheadGui(player)
end)

Explorer:
image

You need to clone the OverheadGui inside the updateOverheadGui function, this way - it’ll clone anytime a player joins.

Also a note, you dont’ need to do OverheadGui.Parent = character:WaitForChild("HumanoidRootPart") it’s useless, you already have an if-statement to check if the player’s character is loaded, so replace it with OverheadGui.Parent = character.HumanoidRootPart

Try this:

local function updateOverheadGui(player)
    local OverheadGui = script.OverheadGui:Clone()
	local character = player.Character

	if character then
		local leaderstats = player:WaitForChild("leaderstats")
		local level = leaderstats:WaitForChild("Level")

		OverheadGui.Username.Text = player.DisplayName .. " (@" .. player.Name .. ")"
		OverheadGui.Level.Text = "Level: " .. level.Value
		OverheadGui.Parent = character.HumanoidRootPart

		level.Changed:Connect(function(newValue)
			OverheadGui.Level.Text = "Level: " .. newValue
		end)
	end
end

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function()
		updateOverheadGui(player)
	end)
	updateOverheadGui(player)
end)
1 Like

Seems like it’s working perfectly now, thank you.

1 Like

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