WaitForChild Behaving Strangely

I’m currently working on a project and while I was making a function to update the players currency the text didn’t seem to change, sometimes it would inform me that the UI wasn’t found and other times nothing happened at all.

local Players = game:GetService("Players")
local PlayerGui = Players.LocalPlayer:WaitForChild("PlayerGui")
local Core = PlayerGui:WaitForChild("Core")

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Events = ReplicatedStorage:WaitForChild("Events")
local UpdateGold = Events:WaitForChild("UpdateGold")

UpdateGold.OnClientEvent:Connect(function(amount)
	Core:WaitForChild("GoldDisplay"):WaitForChild("GoldDisplay").Text = tostring(amount)
end)

Here is a picture of my explorer,

The goal is to update GoldDisplay’s text, for some reason it doesn’t update or error.

The event is caught, if I printed the amount it would print

1 Like

Put that LocalScript in the StarterGui instead, StarterGuis by default remake themselves when the player respawns, so you might be editing an old copy.

This is still resulting in the same problem here’s what I got.

local Core = script.Parent
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Events = ReplicatedStorage:WaitForChild("Events")

Events:WaitForChild("UpdateGold").OnClientEvent:Connect(function(amount)
    print(amount)
    Core:WaitForChild("GoldDisplay"):WaitForChild("GoldDisplay").Text = tostring(amount)
end)

Maybe a screenshot of the output will help? Include these:

  • Warnings from :WaitForChild()
  • Any print()

As I said, it prints “Amount” and there are no warnings at all.

1 Like
local Core = script.Parent
local GoldDisplay = Core:WaitForChild("GoldDisplay")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Events = ReplicatedStorage:WaitForChild("Events")

Events:WaitForChild("UpdateGold").OnClientEvent:Connect(function(amount)
    print(amount)
    GoldDisplay:WaitForChild("GoldDisplay").Text = tostring(amount)
end)

Try this.

Tried this an still have the same results.

Can u try to print full name of GoldDisplay?

I printed it, and it printed the name.

1 Like

I mean print full name of it like

print(GoldDisplay:GetFullName())
  Players.NodeSupport.PlayerGui.Core.GoldDisplay (x2)
1 Like

Try this too

print(GoldDisplay["GoldDisplay"].Name)

GoldDisplay (x2)

1 Like
local Core = script.Parent
local GoldDisplay = Core:WaitForChild("GoldDisplay")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Events = ReplicatedStorage:WaitForChild("Events")

Events:WaitForChild("UpdateGold").OnClientEvent:Connect(function(amount)
    print(amount)
    GoldDisplay["GoldDisplay"].Text = tostring(amount)
end)

Hope this work.

I was hoping as well, sadly this didn’t work.

Well since Roblox takes time to load it could be that the UI hasn’t fully loaded. so it would make sense to add a wait(). For instance,

wait() 

local Players = game:GetService("Players")
local PlayerGui = Players.LocalPlayer:WaitForChild("PlayerGui")
local Core = PlayerGui:WaitForChild("Core")

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Events = ReplicatedStorage:WaitForChild("Events")
local UpdateGold = Events:WaitForChild("UpdateGold")

UpdateGold.OnClientEvent:Connect(function(amount)
	Core:WaitForChild("GoldDisplay"):WaitForChild("GoldDisplay").Text = tostring(amount)
end)

You can also do…

repeat wait() until game:IsLoaded()

local Players = game:GetService("Players")
local PlayerGui = Players.LocalPlayer:WaitForChild("PlayerGui")
local Core = PlayerGui:WaitForChild("Core")

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Events = ReplicatedStorage:WaitForChild("Events")
local UpdateGold = Events:WaitForChild("UpdateGold")

UpdateGold.OnClientEvent:Connect(function(amount)
	Core:WaitForChild("GoldDisplay"):WaitForChild("GoldDisplay").Text = tostring(amount)
end)
2 Likes

I don’t think this is a :WaitForChild() problem. Try printing: print(GoldDisplay["GoldDisplay"].Text) both before and after the change of the text.

@NYDynamics You don’t really need the wait() or yield there. Because the thread is suspended upon :WaitForChild().

Interesting, the post above worked. I didn’t think of that. Rewriting assertions…

Send the code of the calling of the event.

That’s not really necessary either:

The event fired is alright.


I’ll check tomorrow and see if I can update the text through one of my UIs.