Leaderboard not working properly

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

  1. What do you want to achieve? Keep it simple and clear!
    A leaderboard for NPCs that shows how many rounds they survived.
  2. What is the issue? Include screenshots / videos if possible!
    When in a server or playtesting, it doesn’t appear, with a single error in the Client Console without any details.
  3. What solutions have you tried so far? Did you look for solutions on the Creator Hub?
    Tweaking sections of the code, yes.
    After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!
local CollectionService = game:GetService("CollectionService")

local scrollingFrame = script.Parent
local npcLabelTemplate = script.NPC

local function onNpcAdded(npc)
	if not npc:IsDescendantOf(workspace.AllBuilders) then
		return
	end

	local npcLabel = npcLabelTemplate:Clone()
	npc.Humanoid.Died:Once(function()
		npcLabel:Destroy()
	end)
	local survivedTextLabel = npc.Head.ROUNDS.SURVIVED
	local function updateLabel()
		local roundsSurvived = tonumber(npc.Head.ROUNDS.SURVIVED.Text:match("%d+"))
		if roundsSurvived then
			npcLabel.Text = `{roundsSurvived}: {npc.Name}`
			npcLabel.LayoutOrder = -roundsSurvived
		end
	end
	
	npc.Head.ROUNDS.SURVIVED:GetPropertyChangedSignal("Text"):Connect(updateLabel)
	updateLabel()
	npcLabel.Parent = scrollingFrame
	print("Added")
end

for _, npc in CollectionService:GetTagged("NPC") do
	onNpcAdded(npc)
end

CollectionService:GetInstanceAddedSignal("NPC"):Connect(onNpcAdded)

Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.

1 Like

A coder’s favorite thing to do, put prints in your function in different places. Start at the top of the local onNpcAdded function, and then move throughout it to make sure nothing is going on.

2 Likes

This looks like a ServerScript set up.. If you’re running this from ServerScriptService, GUI elements won’t appear. Use a LocalScript under the player’s GUI.

The logic and syntax look correct, but this is a ServerScript trying to manipulate GUI elements, which must be done in a LocalScript.

I ran it as a LocalScript now, and i got an error. Line 15, Head is not a valid member of model “Workspace.AllBuilders.brick man man brick”.

Its called debugging… (charchar)

Is gui a screen gui or something on a part?

Exactly, you have to figure out where you code goes wrong.

It is a ScrollingFrame inside of a ScreenGUI.

Okay then this should be a client sided script. I almost feel like it would be better if you handled all the round status on the server and then fire all clients with updates.

1 Like

It already is a LocalScript. (char)

Sound like the NPC model doesn’t have a part named Head. Possibly check for Head before indexing..

if not npc:FindFirstChild("Head") then return end

if not npc.Head:FindFirstChild("ROUNDS") or not npc.Head.ROUNDS:FindFirstChild("SURVIVED") then return end

Of course it has a Head. (char)

Also, the problem was fixed, however, the names of the NPCs dont seem to get ordered by how many rounds they have and theyre all put in the same position.

1 Like

One would think… however, this looks like it’s running after the NPC died.

Its a zero-player game where NPCs build to live, try it to see how it works. BUILDERS N' BOULDERS (indiana jones style) - Roblox

1 Like

Sweet, I was just trouble shooting.. Glad you figured it out!

No, its still bugged. (charchar)

I was thinking you may be using a ragdoll death and lost your head.. :rofl:
Sorry, I’m a bit lost with what you’re doing here.. Anyways use a LocalScript when working with the player GUI. AC_Starmarine seems to understand what you’re doing a bit more than I do.. I’m going to back off.

i mean by how in the leaderboard, the names of the NPCs dont seem to get ordered by how many rounds they have and theyre all put in the same position.

i am seriously trying to fix a problem.