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

Datastore not saving, what or how do i do to fix it?

Asked by 3 years ago
game.Players.PlayerAdded:Connect(function(player)
    local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player


local points = Instance.new("IntValue")
points.Name = "Points"
points.Parent = leaderstats

local data 
local success, errormessage = pcall(function()
    data = myDataStore:GetAsync(player.UserId.."-points")
end)

if success then
    cash.Value = data
else
    print("There was an error while getting your data")
    warn(errormessage)
    end

end)

game.Players.PlayerRemoving:Connect(function(player)
    local success, errormessage = pcall(function()
    data = myDataStore:SetAsync(player.UserId.."-points",player.leaderstats.points.Value)
    end)
    if success then
    print("Player Data successfully saved!")
else
    print("There was an error when saving your data")
    warn(errormessage)
    end

end)





Thanks for helping!

0
Did you define "myDataStore"? ParticularlyPenguin 71 — 3y
0
I dont think so, should i do local myDataStore = data? loner_cactus 0 — 3y
0
Please show your ENTIRE script, thanks. ParticularlyPenguin 71 — 3y
0
this is the entire script loner_cactus 0 — 3y
0
Ohh. ParticularlyPenguin 71 — 3y

2 answers

Log in to vote
0
Answered by 3 years ago

Since this is the entire script, you simply forgot to add the datastore variable.

Here is what you need to add!

local DSS = game:GetService("DataStoreService")
local myDataStore = DSS:GetDataStore("myDataStore")

Simply add this to the top of your script and it should work.

0
^^^ Should solve your problem. THUNDER_WOW 203 — 3y
Ad
Log in to vote
0
Answered by 3 years ago

I'll just add to ParticularlyPenguin's answer. A lot of people also have this problem and it's that if you're playing solo in Roblox Studio, or you could also be the last player in a server, or the server could just shut down unexpectedly, but when you leave, the server may shut down before your functions such as PlayerRemoving finishes. If that function does not run then your player's data will not be saved. This is why it is always best to use a function called :BindToClose for saving data when a player leaves. It will allow all bound functions to run before the server shuts down. I've edited your script and you will see how it works. Lastly, on line 27, you did not capitalize "Points" in "player.leaderstats.Points.Value". "points" is not a valid member of leaderstats so it's important that you have the capitalization right.

local DSS = game:GetService("DataStoreService")
local myDataStore = DSS:GetDataStore("myDataStore")

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


local points = Instance.new("IntValue")
points.Name = "Points"
points.Parent = leaderstats

local data 
local success, errormessage = pcall(function()
    data = myDataStore:GetAsync(player.UserId.."-points")
end)

if success then
    cash.Value = data
else
    print("There was an error while getting your data")
    warn(errormessage)
    end

end)

game.Players.PlayerRemoving:Connect(function(player)
    local success, errormessage = pcall(function()
    data = myDataStore:SetAsync(player.UserId.."-points",player.leaderstats.Points.Value)
    end)
    if success then
    print("Player Data successfully saved!")
else
    print("There was an error when saving your data")
    warn(errormessage)
    end

end)

game:BindToClose(function()
    for _, player in pairs(game.Players:GetPlayers()) do -- loop through all the players in the server so every player's data is saved
        local success, errormessage = pcall(function()
            data = myDataStore:SetAsync(player.UserId.."-points",player.leaderstats.Points.Value)
            end)
        if success then
            print("Player Data successfully saved!")
        else
                print("There was an error when saving your data")
                warn(errormessage)
        end
    end
end)

Answer this question