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

DataStores won't save?

Asked by 9 years ago
local Money = game:GetService("DataStoreService"):GetDataStore("Money")

game:GetService("Players").PlayerAdded:connect(function(plyr)
    local bank = "user_"..plyr.userId --Using the player's name glitches out somehow.
    while wait(180) do
        if plyr.PlayerGui.MoneyGui:WaitForChild("Money") then
            plyr.PlayerGui.MoneyGui.Money.Text = "$"..tostring(plyr.PlayerGui.MoneyGui.Money.Text:sub(2)+5)
        end
    end
    wait(.5)
    plyr.PlayerGui.MoneyGui.Money.Text = Money:GetAsync(bank)
    print("Loaded Your Money")
    plyr.CharacterAdded:connect(function(char)
        wait(.5)
        char.Humanoid.Died:connect(function()
            Money:SetAsync(bank, plyr.PlayerGui.MoneyGui.Money.Text)
            print("Saved Your Money")
        end)
        plyr.PlayerGui.MoneyGui.Money.Text = Money:GetAsync(bank)
        print("Loaded Your Money")
    end)
    plyr.PlayerGui.MoneyGui.Money.Changed:connect(function()
        Money:SetAsync(bank, plyr.PlayerGui.MoneyGui.Money.Text)
        print("Saved Your Money")
    end)
end)

It won't print anything or save anything. When I reset my character , the gui doesn't change to the amount of money you have saved. Please help!

2 answers

Log in to vote
1
Answered by 9 years ago

It's because you put an infinite loop halfway through the script... When you put:

while wait(180) do
...
end

You basically make it so every 180 seconds ... happens.. which breaks the script. Nothing after that code will be executed.

To fix this, you can move that loop to the bottom of the script (before the last end)) or use a separate thread for it.

0
It doesn't "break" the script, else the "while" loop will not run at all. Rather, the thread is stuck in the loop forever. Redbullusa 1580 — 9y
0
I would have to do a spawn function. mkay thx. It's supposed to loop infinitely. EzraNehemiah_TF2 3552 — 9y
0
you could create a coroutine for the while loop FutureWebsiteOwner 270 — 9y
0
@Lord @Future That's what I meant when saying "use a seperate thread". The Spawn function creates a new thread, and so do coroutines. Vlatkovski 320 — 9y
Ad
Log in to vote
-2
Answered by
Andorks 23
9 years ago

Did you try in-game? Sometimes studio is weird about that stuff.

Answer this question