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

Script works perfectly in Studio, doesn't work in game. (:WaitForChild()) used. [?]

Here's the code;

local plr = game.Players.LocalPlayer
local Gold = plr:WaitForChild('PlrStats').Gold
local GoldText = script.Parent:WaitForChild('Extras').GoldAmount
local Earn = script.Parent:WaitForChild('Extras').GoldAmount.Earn
local AOP = game.ReplicatedStorage:WaitForChild('AmountOfPlayers')
print('variables')

wait()
AOP.Changed:Connect(function()
	print('2 players joined')
	if AOP.Value == 2 then
		while true do
			print('Giving Gold')
			wait(1)
			Gold.Value = Gold.Value + Earn.Value 
		end
	end
end)

Gold.Changed:Connect(function()
	GoldText.Text = 'Gold: '..Gold.Value
end)

Some info;

This is a LocalScript inside ScreenGui Inside Frame. No FE (FilteringEnabled) No errors in output or F9 in game.

any feedback, help, and support are appreciated.

2 answers

✓ Accepted answer

This should be split into two scripts: one for the Client (LocalScript updating the GUI Text) and one for the Server (Script updating the Gold values).

You should also be using Filtering 100% of the time, but I digress.

First, the LocalScript:

local plr = game.Players.LocalPlayer
local Gold = plr:WaitForChild('PlrStats'):WaitForChild("Gold")
local GoldText = script.Parent:WaitForChild('Extras'):WaitForChild("GoldAmount")

Gold.Changed:Connect(function()
    GoldText.Text = 'Gold: '..Gold.Value
end)

And the Script:

-- local Earn = script.Parent:WaitForChild('Extras').GoldAmount.Earn
-- I'm not sure what your hierarchy looks like, but this should be
-- accessible somewhere outside of the PlayerGui so that the Server
-- can see it.
local AOP = game.ReplicatedStorage.AmountOfPlayers
-- I've told you before that you can use game.Players.NumPlayers for this.

while true do
	if AOP.Value < 2 then
		game.Players.PlayerAdded:wait()
	else
		for _, plr in pairs(game.Players:GetPlayers()) do
			plr.PlrStats.Gold.Value = plr.PlrStats.Gold.Value + Earn.Value
		end
		wait(1)
	end
end

Thats strange. But you can use an alternative and use a player added event and doing #game.Players:GetPlayers().

local plr = game.Players.LocalPlayer
local Gold = plr:WaitForChild('PlrStats').Gold
local GoldText = script.Parent:WaitForChild('Extras').GoldAmount
local Earn = script.Parent:WaitForChild('Extras').GoldAmount.Earn
print('variables')

wait()
game.Players.PlayerAdded:Connect(function()
    print('2 players joined')
    if #game.Players:GetPlayers() == 2 then
        while true do
            print('Giving Gold')
            wait(1)
            Gold.Value = Gold.Value + Earn.Value 
        end
    end
end)

Gold.Changed:Connect(function()
    GoldText.Text = 'Gold: '..Gold.Value
end)

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