Scoreboard will not update

  1. What do you want to achieve? Keep it simple and clear!
    fix my scoreboard bilboardgui

  2. What is the issue? Include screenshots / videos if possible!
    My scoreboard that i made works and all, but it wont update the image and text when asked to.

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    i have tried the developer hub, and looking online for no results

My Code:

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

local ElevatorTeam = Teams:WaitForChild("Elevator")
local LobbyTeam = Teams:WaitForChild("Lobby")

local part = script.Parent

local leaderboardCanChange = true
local maxDistance = 10000

while true do
	wait(1)
	local nearestPlayer, nearestDistance
	for _, player in pairs(Players:GetPlayers()) do
		local character = player.Character
		local distance = player:DistanceFromCharacter(part.Position)
		if not character or 
			distance > maxDistance or
			(nearestDistance and distance >= nearestDistance)
		then
			continue
		end
		nearestDistance = distance
		nearestPlayer = player
	end
	
	if nearestPlayer ~= nil and nearestPlayer.Team == ElevatorTeam and leaderboardCanChange == true then
		local userId = nearestPlayer.UserId
		if userId ~= 0 then
			local thumbType = Enum.ThumbnailType.HeadShot
			local thumbSize = Enum.ThumbnailSize.Size420x420
			local content = Players:GetUserThumbnailAsync(userId, thumbType, thumbSize)
			script.Parent.Parent.ScoreBoard.BillboardGui.ImageLabel.Image = content
			script.Parent.Parent.ScoreBoard.BillboardGui.PlayerLabel.Text = nearestPlayer.DisplayName.. " Is In The Lead!"

			-- print("The nearest player is ", nearestPlayer)
		else
			if nearestPlayer ~= nil and nearestPlayer.Team == LobbyTeam then
				-- print("No Players Are Winning.")
				script.Parent.Parent.ScoreBoard.BillboardGui.ImageLabel.Image = 0
				script.Parent.Parent.ScoreBoard.BillboardGui.PlayerLabel.Text = " "
			end
		end
		
		while wait() do
			script.Parent.Touched:Connect(function(hit)
				if hit.Parent:FindFirstChild("Humanoid") and leaderboardCanChange == true then
					leaderboardCanChange = false
					
					script.Parent.Parent.ScoreBoard.BillboardGui.PlayerLabel.Text = nearestPlayer.DisplayName.. " Won The Race!"
					
					script.Parent.Parent.ScoreBoard.ParticleEmitter1.Enabled = true
					script.Parent.Parent.ScoreBoard.ParticleEmitter2.Enabled = true
					script.Parent.Parent.ScoreBoard.ParticleEmitter3.Enabled = true
					script.Parent.Parent.ScoreBoard.ParticleEmitter4.Enabled = true
					script.Parent.Parent.ScoreBoard.ParticleEmitter5.Enabled = true
					print("Winner Was " ..nearestPlayer.DisplayName)
					
					task.wait(20)
					script.Parent.Parent.ScoreBoard.ParticleEmitter1.Enabled = false
					script.Parent.Parent.ScoreBoard.ParticleEmitter2.Enabled = false
					script.Parent.Parent.ScoreBoard.ParticleEmitter3.Enabled = false
					script.Parent.Parent.ScoreBoard.ParticleEmitter4.Enabled = false
					script.Parent.Parent.ScoreBoard.ParticleEmitter5.Enabled = false
				end
			end)
		end
	end
end

On dying you arnt apart of the elevator team anymore. so it should be blank with no image but it keeps the same. passing each other in game wont switch it either.

PS: I am aware that the particle emitter code is buggy.

3 Likes

what is the parent of the script?
leaderboardCanChange is turned off when its touched. (or do i not get whats wrong there)
also please do not use a while loop then create a connection inside it. it will create a ton of connections and your not even disconnecting them which will cause a memory leak in your game

2 Likes

image

leaderboardCanChange is turned off when touched is so it can determine a winner. first to touch the part wins the race
I will try to fix the memory leak thing soon.

1 Like

Is the leaderboard the “someplayer Is In The Lead”?
If it is try testing using 2 players
If you want the player to not be in the lead when they die then use a while loop to check if they are dead
I can’t seem to get whats wrong
(im blind i didnt read the title lol)

2 Likes

here is the scoreboard

with two players the closest person to the part will be put on there and then wont update at all. it will stay on the first person forever.

i dont need to check if they had died or not. on dying your team gets set to lobby. in the code it only checks: if the nearest player ~= nil & if the nearest player is apart of the elevator team (they arnt if they die)

2 Likes
local userId = nearestPlayer.UserId
		if userId ~= 0 then

maybe this is the issue.
try doing userId ~= nil instead of 0 because 0 is a number not nil
and if there are no nearby players then the userid will be nil not 0 (my guess, i don’t really know too whats the issue so im just tryna help).

2 Likes

I have tested it with 2 players and it will not update still. it wont change at all :frowning:

It says player1 and wont change to player 2.

2 Likes
	local nearestPlayer, nearestDistance
	for _, player in pairs(Players:GetPlayers()) do
		local character = player.Character
		if character then
			local distance = player:DistanceFromCharacter(part.Position)
			if distance <= maxDistance and (not nearestDistance or distance < nearestDistance) then
				nearestDistance = distance
				nearestPlayer = player
			end
		end
	end

idk lol i just rewrote the code that gets the nearest player, you can wait for someone else to come who knows better than me.

2 Likes

Alright, Just tried your code and it did not work. Thank you lots though for trying! Hope the best.

2 Likes

You can try adding print to every if statement in your code.

1 Like

Hopefully this script fixes your problem:

local Players = game:GetService("Players")
local RunService = game:GetService("RunService")

local part = script.Parent

local scoreBoard = part.Parent.ScoreBoard

local billboardGui = scoreBoard.BillboardGui
local imageLabel = billboardGui.ImageLabel
local playerLabel = billboardGui.PlayerLabel

local emitters = {
	scoreBoard.ParticleEmitter1,
	scoreBoard.ParticleEmitter2,
	scoreBoard.ParticleEmitter3,
	scoreBoard.ParticleEmitter4,
	scoreBoard.ParticleEmitter5}:: {ParticleEmitter}


local raceIsActive = false
local heartbeatConnection
local touchedConnection


local function setImageAndText(userId: number, text: string)
	local success, userThumbnail = pcall(
		Players.GetUserThumbnailAsync,
		Players,
		userId,
		Enum.ThumbnailType.HeadShot,
		Enum.ThumbnailSize.Size420x420)

	if success then
		imageLabel.Image = userThumbnail
		playerLabel.Text = text
	else
		imageLabel.Image = ''
		playerLabel.Text = ''
	end
end

local function showParticles()
	for _, emitter in emitters do emitter.Enabled = true end
	task.wait(20)
	for _, emitter in emitters do emitter.Enabled = false end
end

local function stopRace()
	if raceIsActive then
		touchedConnection:Disconnect()
		touchedConnection = nil

		heartbeatConnection:Disconnect()
		heartbeatConnection = nil

		raceIsActive = false
	end
end

local function onTouched(basePart: BasePart)
	local model = basePart:FindFirstAncestorOfClass("Model")

	if model then
		local player = Players:GetPlayerFromCharacter(model)

		if player then
			stopRace()
			print("Winner Was "..player.DisplayName)
			setImageAndText(player.UserId, player.DisplayName.." Won The Race!")
			showParticles()
		end
	end
end

local function onHeartbeat()
	local winningPlayer
	local winningPlayerDistance = math.huge

	for _, player in Players:GetPlayers() do
		if player.Character then
			local distance = player:DistanceFromCharacter(part.Position)

			if distance < winningPlayerDistance then
				winningPlayer = player
				winningPlayerDistance = distance
			end
		end
	end

	setImageAndText(winningPlayer.UserId, winningPlayer.DisplayName.." Is In The Lead!")
end

local function startRace()
	if raceIsActive then return end
	raceIsActive = true

	touchedConnection = part.Touched:Connect(onTouched)
	heartbeatConnection = RunService.Heartbeat:Connect(onHeartbeat)
end

startRace() -- Call this function when you'd like to start the race

Unfortunately I wasn’t able to test it, though

By the way, you currently have a massive memory leak here:

You’re creating a brand new connection within an infinite loop that’s within another infinite loop, without ever disconnecting the connections. I strongly recommend that you get the habit of creating connections outside of any infinite loop as it’s rarely ever required (connections stay alive until you either disconnect them, their Instance is destroyed, or were created using :Once) and you’ll need to be extremely careful to avoid memory leaks if you do so

3 Likes

HAHA YOU DID IT!

it works perfectly now. Thank you so much.

I dont know what did it but the Text and image changes to who is closer to the part.

Touching the part will stop all loops and change the text to say who has won!
Thank you really.

2 Likes

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