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