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

Datastores do not save for some reason?

Asked by 4 years ago

so im trying to make the datastore save but it does not print Player data was saved it also does not save the data. it does not give a error and i have studio api services enabled code:

local DS = game:GetService("DataStoreService")
local SaveMoney = DS:GetDataStore("SaveMoney")
local SaveDiamond = DS:GetDataStore("SaveDiamond")

local function PlayerAdded(Plr)
    local Stats = Instance.new("Folder", Plr)
    Stats.Name = "stats"

    local Money = Instance.new("IntValue", Stats)
    Money.Name = "Money"
    Money.Value = SaveMoney:GetAsync(Plr.UserId) or 500
        local Mone2y = Instance.new("IntValue", Stats)
    Mone2y.Name = "diam"
    Mone2y.Value = SaveDiamond:GetAsync(Plr.UserId) or 0

    while true do
        wait(300)
        local MyGameStuff = script.Moneyz:Clone()
        local ValueLol = math.random(10,285)
        MyGameStuff.Frame.amount.Text = ValueLol
        MyGameStuff.Frame.amount.CashToGive.Value = ValueLol
        MyGameStuff.Parent = Plr.PlayerGui
end
end



local function PlayerRemoving(Plr)
    local success, errormessage = pcall(function()
    SaveMoney:SetAsync(Plr.stats.Money.Value, Plr.UserId)
    SaveDiamond:SetAsync(Plr.stats.diam.Value, Plr.UserId)
    end)
    if success then
        print("Player data was saved")
    else
        warn(errormessage)
    end
    end

game.Players.PlayerAdded:Connect(PlayerAdded)
game.Players.PlayerRemoving:Connect(PlayerRemoving)
0
if youre doing it in studio it doesnt connect on player removed it will be fine ingame try making a private version misha123 83 — 4y
0
Well it’s most likely because you’re either trying it out on studio or because the server hadn’t got enough time to save before it shuts down. I’d recommend using BindToClose so it will save. Crafter215 -5 — 4y
0
Do not use separate DataStores to save individual stats. I recommend you look into DataStore2 or ProfileService when you want an easy way to store data. Memotag 226 — 4y

3 answers

Log in to vote
1
Answered by
iOwn_You 543 Moderation Voter
4 years ago

When the last player in the server leaves PlayerRemoving doesnt fire. To save the data of the last player in the server you should use game:BindToClose() This will run the function before the server shuts down and save the data of the last player.

Keep in mind; BindToClose() will only keep the server from shutting down until the function finishes running or until 30 seconds have passed from the moment it started so make sure your function doesn't take that long to execute or you will have dataloss.

BindToClose API

Ad
Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

Well it’s most likely because you’re either trying it out on studio and not on a real server or because the server hasn’t got enough time to save before it shuts down. I’d recommend using BindToClose so it will save.

Log in to vote
0
Answered by
ACHRONlX 255 Moderation Voter
4 years ago

Adding on to what OwnYou said, you can create a seperate thread for each player ingame at the time so that it effectively saves all of their data.

local function Save(Key, Data)
    pcall(function()
        DataStore:SetAsync(Key, Data);
    end)
end

game:BindToClose(function()
    for _,v in pairs(game.Players:GetPlayers()) do
        coroutine.wrap(Save)(v.UserId, true);
    end
end)
0
it does not save for me in studio... do i need to test this in-game? NoobPowerPlayzz 15 — 4y
0
You can add a bool value in serverstorage called SaveInStudio to make sure it saves in studio. ACHRONlX 255 — 4y

Answer this question