What do you want to achieve? Keep it simple and clear!
fix my scoreboard bilboardgui
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.
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.
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
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.
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)
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)
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).
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.
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