Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 4 years ago
Edited 4 years ago

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
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. gullet 471 — 4y

1 answer

Log in to vote
0
Answered by
Despayr 505 Moderation Voter
4 years ago

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)
Ad

Answer this question