Datastoreservice not working

Hello users of devforum. I have been following a couple of tutorials of how to use datastoreservice for my game that is gonna use a level system. The issue im having is. It doesn’t save. Whenever I play and get to a specific level. I replay again and now its back to square one.

local DataStoreService = game:GetService("DataStoreService")
local PlayerData = DataStoreService:GetDataStore("PlayerData")

local function onPlayerJoin(player)
	local CurrentLevel = player:WaitForChild("Level")
	local CurrentEXP = CurrentLevel:WaitForChild("Current")
	local MaximumEXP = CurrentLevel:WaitForChild("Max")
	
	local playerUserID = "Player_".. player.UserId
	local data = PlayerData:GetAsync(playerUserID)
	
	if data then
		CurrentLevel.Value = data
		CurrentEXP.Value = data
		MaximumEXP.Value = data
	else
		CurrentLevel.Value = 1
		CurrentEXP.Value = 0
		MaximumEXP.Value = 100
	end
end

local function onPlayerExit(player)
	local success, err = pcall(function()
		local playerUserId = "Player"..player.UserId
		PlayerData:SetAsync(playerUserId, player.Level.Value, player.Level.Current.Value, player.Level.Max.Value)
	end)
	if not success then
		warn("Data failed to save")
	end
end

game.Players.PlayerAdded:Connect(onPlayerJoin)
game.Players.PlayerRemoving:Connect(onPlayerExit)
1 Like

Now that I notice I just noticed that the warn function data failed to save is printing.

Need to print the err instead
warn(err)
Then we can help u debug

You testing this on studio? Anyway, try using BindToClose!

can you send us the output ? is there any error ?

the problem is that youre saving data with 3 values, you must give only one value, try this:

PlayerData:SetAsync(playerUserId,(player.Level.Value.."-"..player.Level.Current.Value.."-"..player.Level.Max.Value))

they when you take the data split it with “-”

This might work for saving multiple stats:

local dataStoreService = game:GetService("DataStoreService")
local dataStore = dataStoreService:GetDataStore("PlayerData")

game.Players.PlayerAdded:Connect(function(playerInstance)
	local statsFolder = Instance.new("Folder", playerInstance)
	statsFolder.Name = "Level"

	local currentExp = Instance.new("IntValue", statsFolder)
	currentExp.Name = "Current"

	local maximumExp = Instance.new("IntValue", statsFolder)
	maximumExp.Name = "Maximum"

	-- Loading Data
	local playerId = playerInstance.UserId
	local playerData = dataStore:GetAsync(playerId)
	if playerData then
		-- Loading Saved Data
		currentExp.Value = playerData['Current']
		maximumExp.Value = playerData['Maximum']

		-- Log
		print("Loaded Saved Data")
	else
		-- Loading Default Data
		currentExp.Value = 0
		maximumExp.Value = 0

		-- Log
		print("Loaded Default Data")
	end
end)

game.Players.PlayerRemoving:Connect(function(playerInstance)
	local playerId = playerInstance.UserId

	-- Creating Table
	local function create_table(playerInstance)
		local p_stats = {}
		for _, stats in pairs(playerInstance.Level:GetChildren()) do
			p_stats[stats.Name] = stats.Value
		end
		return p_stats
	end

	-- Saving
	local function onplayerExit(playerInstance)
		local p_stats = create_table(playerInstance)
		local s, e = pcall(function()
			dataStore:SetAsync(playerId, p_stats)
			print("Saved Data")
		end)
		if not s then
			warn("Couldn't Save Data For: "..playerId)
		end
	end

	onplayerExit(playerInstance)
end)

Put this in a Script in ServerScriptService and it should work.
Also make sure API Services have access to the game.

Edit: I tested it in a game and it worked fine for me.

1 Like

you cant save tables into data store

It worked just fine for me so ig you can?

is it saves the data correctly and it works fine ?

You can actually, i have done that multiple times now and it works…

1 Like

SOrry for not responding, went to sleep early and aalso had school but I can test this out.

while the prints do work it doesnt not seem to actually save the data itself. I can see that whenever I get to like for say level 3. I stop the game. Then join back and its back to level 1.

Yes i am testing this on studio

aaaand now for some reason the function for the player to automatically get 25 xp after every 5 seconds or so doesn’t work when I join the game via the website but works whenver I launch in via studio…

You can save tables and dictionaries, they are converted into JSON on Roblox servers and stored as strings.

1 Like

Are API services on? And is that the only leaderstats kind of script, or are there more that could interfere with it?

I made my version into a model, you can get it along with instructions here:

I made a saving stats script that is fully configurable and supports multiple stats!

You can get it into your game by just getting the model and inserting it, just keep in mind that it could not work if you already have a stats script interfering with it.

  1. You had a typo in “Player_” at the bottom part.
  2. Convert it to a Table. I have tested the save for some time now and, it seems to be running smoothly now.
local DataStoreService = game:GetService("DataStoreService")
local PlayerData = DataStoreService:GetDataStore("PlayerData")

local function onPlayerJoin(player)
	local CurrentLevel = player:WaitForChild("Level")
	local CurrentEXP = CurrentLevel:WaitForChild("Current")
	local MaximumEXP = CurrentLevel:WaitForChild("Max")

	local playerUserID = "Player_".. player.UserId
	local data = PlayerData:GetAsync(playerUserID)

	if data then
		CurrentLevel.Value = data
		CurrentEXP.Value = data
		MaximumEXP.Value = data
	else
		CurrentLevel.Value = 1
		CurrentEXP.Value = 0
		MaximumEXP.Value = 100
	end
end

local function onPlayerExit(player)
	local success, err = pcall(function()
		local playerUserId = "Player_"..player.UserId
		local valuetoSave = {player.Level.Value, player.Level.Current.Value, player.Level.Max.Value}
		PlayerData:SetAsync(playerUserId, valuetoSave)
	end)
	if not success then
		warn(err)
	end
end

game.Players.PlayerAdded:Connect(onPlayerJoin)
game.Players.PlayerRemoving:Connect(onPlayerExit)