Scripting Question

Im trying to make a tycoon and right now I’m working on displaying the money (on a surface gui).

Im using a global variable to find the player.

local player = _G.Owner

local UncollectedMoney = player.UncollectedMoney

Is there a way to wait for the variable to be filled? Since not someone will join right away and the UncollectedMoney will just print player = nil

Any questions comment, thanks!

Unrelated, but why not just use a ModuleScript?

Just call WaitForChild() which would make the variable yield, attempting to find the UncollectedMoney inside the Player object until its fully found

1 Like

please do not use _G, just use an module script

1 Like

No. There is no good way to wait for a variable to change. In theory yes you could. However, I highly do not recommend it as waiting for a variable to change is always unnecessary as events handle this already.

Instead, you can wait for a player to join the game.

There is a service called Players and one event of that service is called Players | Roblox Creator Documentation

game:GetService("Players").PlayerAdded:Connect(function(Player)
    --Do what you want to do here when player joins.
end)