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

Cant get Save Script to save stuff?

Asked by 3 years ago

Im trying to make my simulator save but my code is not working no errors show up when tested.

local DS = game:GetService("DataStoreService"):GetDataStore("SavaData")
game.Players.PlayerAdded:Connect(function(plr)
    wait()
    local plrkey = "id_"..plr.userId
    local save1 = plr.leaderstats.Crumbs
    local save2 = plr.leaderstats.Coins

    local GetSaved = DS:GetAsync(plrkey)
    if GetSaved then
        save1.Value = GetSaved[1]
        save2.Value = GetSaved[2]
    else
        local NumberForSaving = {save1.Value, save2.Value}
        DS:GetAsync(plrkey,NumberForSaving)
    end
end)

game.Players.PlayerRemoving:Connect(function(plr)
    DS:SetAsync("id_"..plr.userId,{plr.leaderstats.Crumbs.Value, plr.leaderstats.Coins.Value})
end)
0
what exactly isnt working? sata5pa3da 286 — 3y
0
The values that are needing to be saved are returning to beggining value Meerkatsrawesome 1 — 3y

2 answers

Log in to vote
0
Answered by
JesseSong 3916 Moderation Voter Community Moderator
3 years ago
Edited 3 years ago

Improvements:

  • When indexing a player's user id use plr.UserId not plr.userid
  • It's better to put the leaderstats and the datastore in the same script so that it saves time and uses fewer lines.

Those are the only errors/bugs I can see in your code.

Pro-tip:

When debugging code, use print statements to help you know what lines are running and what lines aren't.

Make sure this is a Normal Script.

(Also, make sure your API service is on. You can check this by going on game settings < Security and then enable it.)

Fixed Script:

local DS = game:GetService("DataStoreService"):GetDataStore("SavaData")

game.Players.PlayerAdded:Connect(function(player)
    local Folder = Instance.new("Folder",player)
    Folder.Name = "leaderstats" 

    local Coins = Instance.new("IntValue",Folder)
    Coins.Name = "Coins"

    local Crumbs = Instance.new("IntValue",Folder)
    Crumbs.Name = "Crumbs"

    local plrkey = "id_"..player.UserId
    local save1 = player.leaderstats.Crumbs
    local save2 = player.leaderstats.Coins

    local GetSaved = DS:GetAsync(plrkey)
    if GetSaved then
        save1.Value = GetSaved[1]
        save2.Value = GetSaved[2]
    else
        local NumberForSaving = {save1.Value, save2.Value}
        DS:GetAsync(plrkey,NumberForSaving)
    end
end)

game.Players.PlayerRemoving:Connect(function(plr)
    DS:SetAsync("id_"..plr.UserId,{plr.leaderstats.Crumbs.Value, plr.leaderstats.Coins.Value})
end)

Recommendation(s):

It's also better to use DataModel:BindToClose, as this function gets called when the game shuts down. <-------------------------------------------------------------------------------------------------------->

Re-edited: (I also added another variable that references the Datastore, I also added pcalls to ensure the code runs through any errors.)

Note: You don't need to add BindToClose on line 30, you could've added it on the bottom of the script, but I just decided to do it like this:

Re-edited Script:

local DS = game:GetService("DataStoreService")
local myDataStore = DS:GetDataStore("SaveData") -- Added

game.Players.PlayerAdded:Connect(function(player)
    local Folder = Instance.new("Folder",player)
    Folder.Name = "leaderstats" 

    local Coins = Instance.new("IntValue",Folder)
    Coins.Name = "Coins"

    local Crumbs = Instance.new("IntValue",Folder)
    Crumbs.Name = "Crumbs"

    local data
    local playerUserId = "Player__" .. player.UserId    
    local success,errormessage = pcall(function()
        data = myDataStore:GetAsync(playerUserId)

    end)
    if success then
        if data then
            Coins.Value = data.Coins
            Crumbs.Value = data.Crumbs

        end
    end
end)


    game:BindToClose(function()
        print("The games has shutdown")

        for i,plr in pairs(game.Players:GetPlayers()) do
            local data = {
                Coins = plr.leaderstats.Coins.Value;
                Crumbs = plr.leaderstats.Crumbs.Value;
            }
            print("it works")
        end

    end)

game.Players.PlayerRemoving:Connect(function(player)
    local playerUserId = "Player__" .. player.UserId
    local data = {
        Coins = player.leaderstats.Coins.Value;
        Crumbs = player.leaderstats.Crumbs.Value;
    }
        local success,errormessage = pcall(function()
        myDataStore:SetAsync(playerUserId,data)
    end)

    if success then
        print("Data successfully saved!")
    else
        print("Data failed to save!")
    end
end)

If my answer helped you, please accept it!

Fixed grammatical errors

~5/12/2021

Reference:

DataModel:BindToClose

0
More enquires or comments, post here. JesseSong 3916 — 3y
0
what do i replace with DataModel:BindToClose? Meerkatsrawesome 1 — 3y
0
im going to update my answer, wait JesseSong 3916 — 3y
0
also, if my answer helped you, don't forget to accept it JesseSong 3916 — 3y
View all comments (8 more)
0
Re-edited, check my answer JesseSong 3916 — 3y
0
it is saving once but after i try to save it more than once it will just go back to the amount from the first save Meerkatsrawesome 1 — 3y
0
if it helps the (regular) script is in serverscriptservice Meerkatsrawesome 1 — 3y
0
include a video or a snapshot so i can give you assistance. JesseSong 3916 — 3y
0
also, tell me what youre using to change the amount. for example, are you changing it server sided or client sided JesseSong 3916 — 3y
0
this script should work. if it doesn't i will review the code later, but i think ur doing something wrong JesseSong 3916 — 3y
0
if you have more questions comment at 10 AM (GMT) JesseSong 3916 — 3y
0
oops didnt copy a couple of lines on accident now it works thanks :) Meerkatsrawesome 1 — 3y
Ad
Log in to vote
0
Answered by 3 years ago

If there aren't errors, then its possible this won't work because your using studio. When a player leaves, it fires player removing before the server can shut down, this is normal roblox. But with studio, when you hit the stop button, it shuts down the virtual server so it won't actually run player removing most of the time. I have experienced it myself this week and many times before. To make sure it saves, you can save it manually while using studio, make a button or something which upon clicking, will fire to a remote event that saves the data before you click stop which closes the virtual server.

Hope this helps :3

0
ever heard of bindtoclose greatneil80 2647 — 3y
0
No AlexanderYar 788 — 3y
0
use game:BindToClose to resolve your problem: (https://developer.roblox.com/en-us/api-reference/function/DataModel/BindToClose) JesseSong 3916 — 3y

Answer this question