DataStore not working

Hello, I have been trying to get datastores to work for a long time with no luck. I watched a video on data saving, but it didn’t help. The video is almost three years old, so maybe it’s outdated. Even if it is, I can’t find any problems with it.

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

local function onPlayerJoin(player)  -- Runs when players join
	local leaderstats = Instance.new("Folder")  --Sets up leaderstats folder
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player

	local gold = Instance.new("IntValue") --Sets up value for leaderstats
	gold.Name = "Gold"
	gold.Parent = leaderstats

	local playerUserId = "Player_" .. player.UserId  --Gets player ID
	local data = playerData:GetAsync(playerUserId)  --Checks if player has stored data
	
	if data then
		-- Data exists for this player
		gold.Value = data
	else
		-- Data store is working, but no current data for this player
		gold.Value = 0
	end
end

local function onPlayerExit(player)  --Runs when players exit
	local success, err = pcall(function()
		local playerUserId = "Player_" .. player.UserId
		playerData:SetAsync(playerUserId, player.leaderstats.Gold.Value) --Saves player data
	end)
	
	if not success then
		warn('Could not save data!')
	end
end

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

pcall this like you did when the player leaves.

so it’d look like:

local data
local success, err = pcall(function()
   data = playerData:GetAsync(playerUserId)
end)

if data and success then
   ...
else
   warn(err)
   ...
end
1 Like

I’m still learning how to code with Lua in Roblox Studio, so idk if I did it right, but it’s still not working.

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

local function onPlayerJoin(player)  -- Runs when players join
	local leaderstats = Instance.new("Folder")  --Sets up leaderstats folder
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player

	local gold = Instance.new("IntValue") --Sets up value for leaderstats
	gold.Name = "Gold"
	gold.Parent = leaderstats

	local playerUserId = "Player_" .. player.UserId  --Gets player ID
	
	local data
	local success, err = pcall(function()
		data = playerData:GetAsync(playerUserId)
	end)

	if data and success then
		gold.Value = data
	else
		warn(err)
		gold.Value = 0
	end
end

local function onPlayerExit(player)  --Runs when players exit
	local success, err = pcall(function()
		local playerUserId = "Player_" .. player.UserId
		playerData:SetAsync(playerUserId, player.leaderstats.Gold.Value) --Saves player data
	end)
	
	if not success then
		warn('Could not save data!')
	end
end

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

also, I’m testing using another script to change the leaderstat value, I don’t think that would cause the problem, there’s no other scripts besides those two.

Make sure this script is a Script (server-sided) and not a LocalScript (client-sided).
Also I suggest to manually change it by going into the server mode and changing your value from there, then leaving → rejoining and seeing if any data has been saved or not.

Also note any warnings or errors down.

I checked scripts I made myself and they look quite alike, so the only real problem I can think of is you not checking errors or not turning on DataStores:

1 Like

I was testing with a localscript because I tried with a server script but the leaderstat value didn’t change, I just found out that the problem was the script was referring to LocalPlayer which doesn’t work with server scripts. I changed the code and put it in a server script and tested and the data saved. I just overlooked that when I was first testing it. I’ve been trying to get data saving to work for years and it finally worked. Thanks for the help!

1 Like

It happens to the best of us, it’s good to face issues like this and to learn from them.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.