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

Why does this work properly in studio but not publicly?

Asked by 6 years ago

game.Players.PlayerAdded:connect(function(player)

local gold = Instance.new('IntValue', player)
local golds = Instance.new('ScreenGui')
local amount = Instance.new('TextLabel')

golds.Parent = game.Players.LocalPlayer.PlayerGui
golds.Name = "Golds"
golds.Enabled = true
amount.Parent = golds
amount.Active = true
amount.Position = UDim2.new(0.5, 0, 0.5, 0)
amount.TextSize = 30
amount.TextColor3 = BrickColor.new("Gold").Color
gold.Name = "Gold"


while true do
    amount.Text = gold.Value
    wait(1)
end

end)

I'm probably missing something crucial here, but I can't seem to find out what's the problem.

Some help would be greatly appreciated.

0
Check your console in game by hitting F9 or typing /console RubenKan 3615 — 6y

1 answer

Log in to vote
0
Answered by 6 years ago

This was actually a simple mistake that I've made in the past myself. The issue is on line 6 where you have "golds.Parent = game.Players.LocalPlayer.PlayerGui"

game.Players.PlayerAdded:connect(function(player)
local gold = Instance.new('IntValue', player)
local golds = Instance.new('ScreenGui')
local amount = Instance.new('TextLabel')

golds.Parent = player.PlayerGui -- The Issue lies here
golds.Name = "Golds"
golds.Enabled = true
amount.Parent = golds
amount.Active = true
amount.Position = UDim2.new(0.5, 0, 0.5, 0)
amount.TextSize = 30
amount.TextColor3 = BrickColor.new("Gold").Color
gold.Name = "Gold"


while true do
    amount.Text = gold.Value
    gold.Value = gold.Value + math.random(1,2) -- Simply added this to verify in-game that it was working.
    wait(1)
end

end)

So you're attempting to find the player within Players using .LocalPlayer, the problem here is it works only in a LocalScript (However you may have used one), When you're in test mode using .LocalPlayer will work even within a server script so it wont throw the error until you hit Play.

Upon entering with your original script and checking the Dev Console I found this error, https://gyazo.com/69604af782730fd94de3274814e806dd

Another good thing to note, When you set up this function for when a player enters the game, you're already created a variable for their Player, named "player".

If you look at line 1, you already showed this by adding your IntValue for Gold Directly into the player.

That's why it didn't work, I also added in a line for you to go in game and see the variable and text changing in real time without needing to alter the values manually.

Hope this helped!

Ad

Answer this question