Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How do I check for the existence of an object before assigning it a value?

Hi, I am trying to code a gui that is dynamically assigned. I have four teams. I am getting stuck at a certain point. I want to make a function that, when a player joins the game, checks if the other teams have already scored to update their labels. It looks like this:

local function updateAllLabelsLateArrival(redPoints, bluePoints, yellowPoints, greenPoints)
	
	game.Players.LocalPlayer.PlayerGui.ScreenGui.ReallyRedTeam.Points.Text = redPoints
	game.Players.LocalPlayer.PlayerGui.ScreenGui.ReallyBlueTeam.Points.Text = bluePoints 
	game.Players.LocalPlayer.PlayerGui.ScreenGui.NewYellerTeam.Points.Text = yellowPoints
	game.Players.LocalPlayer.PlayerGui.ScreenGui.LimeGreenTeam.Points.Text = greenPoints
	
end

The function is remotely triggered from a server-side script when a player joins. The problem I have is that not all four labels might exist. Suppose a green team player joins in when there is only a red team player already playing. It will come back with the error

ReallyBlueTeam is not a valid member of ScreenGui

I thought wrapping each line in an if statement to check if the label exists, like so:

if game.Players.LocalPlayer.PlayerGui.ScreenGui.ReallyRedTeam then game.Players.LocalPlayer.PlayerGui.ScreenGui.ReallyRedTeam.Points.Text = redPoints end

But this is giving the same error. So my question is, how do I check that a label has been created before updating the points? thanks

1 comment

0

use WaitForChild

1 answer

Try using FindFirstChild to check for it's existence, the dot operator will give you an error if the child is nil

This discussion is preserved as it was. New answers, comments, and votes are closed.