Data store isn't saving

im trying to use datastore but it only load but did not save and i couldn’t find the problem
please help

– data script–

local datastoreservice = game:GetService(“DataStoreService”)
local playerdata = datastoreservice:GetDataStore(“playerdata”)

game.Players.PlayerAdded:Connect(function(player)

local folder = Instance.new("Folder")
folder.Name = "leaderstats"
folder.Parent = player

local cash = Instance.new("IntValue")
cash.Name = "ObbyBux"
cash.Parent = folder

local xp = Instance.new("IntValue")
xp.Name = "Finished"
xp.Parent = folder

local playerId = 'player_'..player.UserId
local data = playerdata:GetAsync(playerId)
if data then
	cash.Value = data['ObbyBux']
	xp.Value = data['Finished']
	print("data loaded")
else
	cash.Value = 0
	xp.Value = 0
	print("New player")
end

end)

local function createtable(player)
local playerstats = {}
for _, stat in pairs(player.leaderstats:GetChildren()) do
playerstats[stat.Name] = stat.Value
end
return playerstats
end

local function onplayerexit(player)
local playerstats = createtable(player)
local success, err pcall(function()
local playerId = ‘player_’…player.UserId
playerdata:SetAsync(playerId, playerstats)
end)
if not success then
print(“data did not save”)
end
end

game.Players.PlayerRemoving:Connect(onplayerexit)

  1. Please indent the script correctly so we can understand better.

  2. [quote=“supercronter4, post:1, topic:828710”]

local data = playerdata:GetAsync(playerId)

if data then
	cash.Value = data['ObbyBux']
	xp.Value = data['Finished']
	print("data loaded")
else
	cash.Value = 0
	xp.Value = 0
	print("New player")
end

[/quote]

You should use a pcall function when GetAsync. Also you should create an empty variable to store the GetAsync.

local data

local success, errorMessage = pcall(function()
    playerdata:GetAsync(playerId)

if success then
    cash.Value = data. ObbyBux or 0 -- 0 is for when the player is new or doesn’t have any data.
    -- load other data here.
else
    warn(“Failed.)
end

  1. [quote=“supercronter4, post:1, topic:828710”]
    local function createtable(player)
    local playerstats = {}
    for _, stat in pairs(player.leaderstats:GetChildren()) do
    playerstats[stat.Name] = stat.Value
    end
    return playerstats
    end
    [/quote]

Remove this.

  1. [quote=“supercronter4, post:1, topic:828710”]
    local function onplayerexit(player)
    local playerstats = createtable(player)
    local success, err pcall(function()
    local playerId = ‘player_’…player.UserId
    playerdata:SetAsync(playerId, playerstats)
    end)
    if not success then
    print(“data did not save”)
    end
    end
    [/quote]

Change this to:

local function onplayerexit(player)
    local leaderstats = player.leaderstats
    local dataToStore = {
        leaderstats.ObbyBux.Value;
        leaderstats.Finished.Value;
    }

    local playerId = "player_"..player.UserId

    local success, errorMessage = pcall(function()
        playerdata:SetAsync(playerId,dataToStore)
    end

    if success then
        print("Successfully saved data!")
    else
        warn("Failed to save data.")
    end
end)

game.Players.PlayerRemoving:Connect(onplayerexit)
  1. Use Pascal style text to understand your code better.

That’s all. Hope it works!

ok ill remember to do that next time :slight_smile:

Try putting

Game:bindtoclose()
Wait(10)
End

In the end of your script

it work now it save data but theres another problem now it dosent load data

That will only works if the server is shut down for like updates or the last person on that server leaves.

Can you try testing it in the game and not studio? Make sure you published it and enable API services for Roblox Studio.

i did i tried everything
i tried it in a game
i turn on API services

Could you do one thing, there are two scripts that I can see above and its pretty hard to understand them because of the formatting. Could you give your whole script in one post along with formatting them? If I am right, something is probably wrong with the Key if its saving but not loading correct data.

local datastoreservice = game:GetService(“DataStoreService”)
local playerdata = datastoreservice:GetDataStore(“playerdata”)

game.Players.PlayerAdded:Connect(function(player)

local folder = Instance.new("Folder")
folder.Name = "leaderstats"
folder.Parent = player

local cash = Instance.new("IntValue")
cash.Name = "ObbyBux"
cash.Parent = folder

local xp = Instance.new("IntValue")
xp.Name = "Finished"
xp.Parent = folder

local data
local playerId  = "player_"..player.UserId

local success, errorMessage pcall(function()
	data = playerdata:GetAsync(playerId)
end)

if success then
	cash.Value = data["ObbyBux"]
	xp.Value = data["Finished"]
	print(success)
else
	warn(errorMessage)
	xp.Value = 0
	cash.Value = 0
	
end

end)

game.Players.PlayerRemoving:Connect(function(player)

local leaderstats = player.leaderstats
local datatostore = {
	leaderstats.ObbyBux.Value;
	leaderstats.Finished.Value;
	}
local playerId = "player_"..player.UserId

local success, errormessage = pcall(function()
	playerdata:SetAsync(playerId, datatostore)
end)
if success then 
	print("Successfully Saved data!")
else
	warn(errormessage)
end

end)

game:BindToClose(function()
wait(5)
end)

There are a couple of things that are doing wrong, but as for the thing not loading its because there isn’t something known as “ObbyBux” or “Finished” in the data table while loading, because while you save it, you save it with numerical indices.

So your new script should look like this:

local datastoreservice = game:GetService(“DataStoreService”)
local playerdata = datastoreservice:GetDataStore(“playerdata”)

game.Players.PlayerAdded:Connect(function(player)

local folder = Instance.new("Folder")
folder.Name = "leaderstats"
folder.Parent = player

local cash = Instance.new("IntValue")
cash.Name = "ObbyBux"
cash.Parent = folder

local xp = Instance.new("IntValue")
xp.Name = "Finished"
xp.Parent = folder

local playerId  = "player_"..player.UserId

local success, response = pcall(playerdata.GetAsync, playerdata, playerId) --You can just do a pcall like this instead of making another anonymous function inside it and then calling GetAsync again inside of it.

if success then
	cash.Value = response[1] or 0 --I normally do this because, if data isn't there in dataStore of the player, it will be set to 0 by default.
	xp.Value = response[2] or 0
else
	warn(response)
	xp.Value = 0
	cash.Value = 0
end
end)

game.Players.PlayerRemoving:Connect(function(player)

local leaderstats = player.leaderstats
local datatostore = {
	leaderstats.ObbyBux.Value;
	leaderstats.Finished.Value;
	}
local playerId = "player_"..player.UserId

local success, errormessage = pcall(function()
	playerdata:SetAsync(playerId, datatostore)
end)

if success then 
	print("Successfully Saved data!")
else
	warn(errormessage)
end
end)

game:BindToClose(function()
wait(5)
end)

You can see what I also did with the pcall for getting the data, you can do the same for it in SetAsync & also that thing that you’re doing in BindToClose function, isn’t really a good practice.

This thread here is very useful in explaining it:

1 Like

it work thank you so much!!!

1 Like

I would also recommend you to go through the thread I have linked, so you can make a BindToClose event function work properly, otherwise your game may face dataloss on Server shutdowns and stuff.

1 Like