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

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

Asked by 6 years ago

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

Log in to vote
0
Answered by
adark 5487 Badge of Merit Moderation Voter Community Moderator
6 years ago

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
Ad
Log in to vote
0
Answered by 6 years ago

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)

Answer this question