Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Why doent this script display the current value on a gui element?

I have recently been making a game, where i need a specific NumberValue to show up on a GUI element. However my script doesn´t seem to work, and the following message is printed in the output: Infinite yield possible on 'Players.AndriusTheGreat:WaitForChild("leaderstats")'. The script is a localscript that lies inside a folder in StarterGui

Here is the script:

game.Players.PlayerAdded:Connect(function(player)
	local StartingMoney = 100
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	local money = Instance.new("NumberValue")
	money.Name = "money"
	money.Parent = leaderstats
	money.Value = StartingMoney
end)

local player = game.Players.LocalPlayer
local leaderstats = player:WaitForChild("leaderstats")
leaderstats.money.Value = 10

game.StarterGui.Guis.ScreenGui.FrameT.TextLabel.Text = leaderstats.money.Value

1 comment

0

That means it might be waiting forever for a child called "leaderstats". Double check it's actually supposed to exist and debug why it doesn't. Check spelling etc.

1 answer

Assuming this is a serverscript, you are trying to define the local player on line 13. Since you are using a global script, this will error as the server cannot tell which player you are referencing.

Second of all, you are trying to access a gui within the startergui, instead if the playergui. Once again, this can only be done via the client (Local Script)

game.Players.PlayerAdded:Connect(function(player)
    local StartingMoney = 100
    local leaderstats = Instance.new("Folder")
    leaderstats.Name = "leaderstats"
    leaderstats.Parent = player

    local money = Instance.new("NumberValue")
    money.Name = "money"
    money.Parent = leaderstats
    money.Value = StartingMoney
end)

local player = game.Players.LocalPlayer
local leaderstats = player:WaitForChild("leaderstats")
leaderstats.money.Value = 10

game.StarterGui.Guis.ScreenGui.FrameT.TextLabel.Text = leaderstats.money.Value

Instead, let's try this.

ServerScript

game.Players.PlayerAdded:Connect(function(player)
    local StartingMoney = 100
    local leaderstats = Instance.new("Folder")
    leaderstats.Name = "leaderstats"
    leaderstats.Parent = player

    local money = Instance.new("NumberValue")
    money.Name = "money"
    money.Parent = leaderstats
    money.Value = StartingMoney
end)

LocalScript --Place in the gui object

local player = game:GetService("Players").LocalPlayer -- use :GetService in case an exploiter changes the name of the service

local leaderstats = player:WaitForChild("leaderstats")

local gui = script.Parent

local label = gui:WaitForChild("FrameT"):WaitForChild("TextLabel")
label.Text = tostring(leaderstats.Money.Value)

leaderstats:WaitForChild("Money").Changed:Connect(function()
	label.Text = tostring(leaderstats.Money.Value)
end)

This discussion is preserved as it was. New answers, comments, and votes are closed.