Ad
Log in to vote
1

Why is my text label not updating?

Asked by 3 years ago
Edited 3 years ago

Hello, I am changing the value of a text label with code so that it equals the number of coins that the player has, but the text label UI is not updating, even though I can print the .text of it and it displays its value.

local Players = game:GetService("Players")

function onPlayerAdded(player)
    local Money = Instance.new("IntValue")
    Money.Name = "Coins"
    Money.Value = 166
    game.StarterGui.ScreenGui.CoinAmount.Text = Money.Value
    print(game.StarterGui.ScreenGui.CoinAmount.Text)
end

--When a player joins, call the onPlayerAdded function
Players.PlayerAdded:connect(onPlayerAdded)

--Call onPlayerAdded for each player already in the game
for _,player in pairs(Players:GetPlayers()) do
     onPlayerAdded(player)
end


0
You are changing and printing the value in the StarterGui, not the player's specific PlayerGui. UgOsMiLy 1074 — 3y
0
I don't see anything called "PlayerGui". bludud1234 40 — 3y
0
What I do see is StarterGui, StarterPack, and StarterPlayer, but I don't see the PlayerGui. bludud1234 40 — 3y
0
PlayerGui is located in game.Players.(Player).PlayerGui. Audiimo 105 — 3y

2 answers

Log in to vote
0
Answered by 3 years ago
Edited 3 years ago

The StarterGui is not what the player see's on their screen. What the player see's is apart of the PlayerGui. The PlayerGui is a folder pasted into each player when they join the server. The folder contains the default settings (unless disabled), like chat, ontop of what is contained in the StarterGui. Updating the StarterGui is only going to edit what players that join the server AFTER you update it see, therefore, you have to edit the player's PlayerGui. To get access to their playerGui, you have to do > game.Players.(player username).PlayerGui.(What you want to get access to) <

This is the new version of your code. Contact me if there are any errors.

local Players = game:GetService("Players")

function onPlayerAdded(player)
    local Money = Instance.new("IntValue")
    Money.Name = "Coins"
    Money.Value = 166
    if player.PlayerGui:FindFIrstChild("ScreenGui") then -- this line isn't needed.
      player.PlayerGui.**.ScreenGui.CoinAmount.Text = Money.Value
    end
end


--When a player joins, call the onPlayerAdded function
Players.PlayerAdded:connect(onPlayerAdded) 

--Call onPlayerAdded for each player already in the game
for _,player in pairs(Players:GetPlayers()) do
     onPlayerAdded(player)
end

0
What is with this FindFirstChild stuff? Why not just say player.PlayerGui.ScreenGui? bludud1234 40 — 3y
0
Sorry I was away. In-game, scripts run much faster than in studio, almost instantly. You can use WaitForChild() and FindFirstChild() methods to make sure it loads. Alternatively, you can use a wait() method superhudhayfa 39 — 3y
0
I am using a fireclient example to create a welcome gui. I, however can not change the color of the "x" button. Can you please help me (Example here: http://tinyurl.com/y2cpjsvg ) TTChaos 749 — 1y
Ad
Log in to vote
0
Answered by
k3du53 110
3 years ago

What the player sees is not controlled through ScreenGui.

ScreenGui: "Children of this object get cloned into a Player's PlayerGui when their character spawns."

local Players = game:GetService("Players")

function onPlayerAdded(player)
    local Money = Instance.new("IntValue")
    Money.Name = "Coins"
    Money.Value = 166
    game.StarterGui.ScreenGui.CoinAmount.Text = Money.Value --if a new player were to join, their screen would show 166 coins, because it is changed in StarterGui
    print(game.StarterGui.ScreenGui.CoinAmount.Text)
end

--When a player joins, call the onPlayerAdded function
Players.PlayerAdded:connect(onPlayerAdded)

--Call onPlayerAdded for each player already in the game
for _,player in pairs(Players:GetPlayers()) do
     onPlayerAdded(player)
end

See PlayerGui vs. StarterGui (wiki.roblox)

Answer this question