Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

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

Asked by 4 years ago

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

0
use WaitForChild User#5423 17 — 4y

1 answer

Log in to vote
0
Answered by
bum5Br 97
4 years ago

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

Ad

Answer this question