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
Put that LocalScript in the StarterGui instead, StarterGuis by default remake themselves when the player respawns, so you might be editing an old copy.
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)