Datasave is not working

  1. What do you want to achieve? I want to do a datasave of multiple values

  2. What is the issue? It’s not saving the value

  3. What solutions have you tried so far? I watch tutorials, i search on devforum, but i cannot find a datasave that is working for my game. Idk what is the issue (btw no errors in output and with another datsave it printed me “Data saved!”, but it didn’t work)

Here is the script (serverscriptservice, normalscript)

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 money = Instance.new("IntValue") --Sets up value for leaderstats
	money.Name = "Points"
	money.Parent = leaderstats



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



	local playerUserId = "Player_" .. player.UserId  --Gets player ID
	local data = playerData:GetAsync(playerUserId)  --Checks if player has stored data

	if data then

		money.Value = data['Money']
		exp.Value = data['Experience']

	else
	-- Data store is working, but no current data for this player

		money.Value = 0
		exp.Value = 0

	end
end



local function create_table(player)

	local player_stats = {}

	for _, stat in pairs(player.leaderstats:GetChildren()) do

		player_stats[stat.Name] = stat.Value

	end

	return player_stats

end



local function onPlayerExit(player)  --Runs when players exit



	local player_stats = create_table(player)

	local success, err = pcall(function()

		local playerUserId = "Player_" .. player.UserId

		playerData:SetAsync(playerUserId, player_stats) --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)

Tysm for answers

how about making this way?

data.Money = money.Value

data.Exp = exp.Value

1 Like

Tysm for the answer, i already try a datasave like that but it didn’t work

it’s strange… maybe you could try getting the player’s stats seperately? Like that:

local leaderstats = player.leaderstats

local exp = leaderstats.exp
local money = leaderstats.money

data.Money = money.Value
data.Exp = exp.Value
1 Like

okk i try it, this video did not help me Advanced Roblox Scripting Tutorial #13 - Data Store / Saving Player Data (Beginner to Pro 2019) - YouTube

btw i’m using the method i sent above so there should be no problems

1 Like

Wrap the :SetAsync and the :GetAsync inside of a pcall function and then tell us what error you get from the error message.

1 Like
local datastoreservice = game:GetService("DataStoreService")
local mystore = datastoreservice:GetDataStore("mydatastore")

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

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


	local gems = Instance.new("IntValue")
	gems.Name = "Gems"
    gems.Parent = leaderstats


	local playerid = "Player_" .. player.UserId
	local data = mystore:GetAsync(playerid)

	if data then
		coins.Value = data['Coins']
		gems.Value = data['Gems']
	else
		coins.Value = 0
		gems.Value = 0
	end
end)


game.Players.PlayerRemoving:Connect(function(player)
	local datatobesaved = {
		Coins = player.leaderstats.Points.Value;
		Gems = player.leaderstats.Gems.Value;

	}
	local playerid = "Player_" .. player.UserId
	local success, err = pcall(function()
		mystore:SetAsync(playerid,datatobesaved)
	end)
	if success then
		print("data saved!")
	else
		print("data faield to save!")
	end
end)

now i tried this datasave, but it is not working look
image
i play and my points are 0 (and it is normal)
image
i touch a part and my points now are 1
image
i leave and it prints me data saved
image
i join again and my points are 0

It is really strange

Ty for the answer, ok i try to do it

Just do the script i wrote above without table, insert it above your set async thats all

1 Like

image
sry i’m new to scripting… i don’t really know where put it xD

Not working :((… Idk why datasave is not working i search a lot of videos, devforum resources but nothing

Ok. So after looking at this code and testing it. There’s nothing wrong at all. The game saved those values for me. May I see the points adding code? (where you add points)

1 Like
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 money = Instance.new("IntValue") --Sets up value for leaderstats
	money.Name = "Points"
	money.Parent = leaderstats

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

	local playerUserId = "Player_" .. player.UserId  --Gets player ID
	local data = playerData:GetAsync(playerUserId)  --Checks if player has stored data

	if data then
		money.Value = data['Points']
		exp.Value = data['Experience']
	else
		-- Data store is working, but no current data for this player
		money.Value = 0
		exp.Value = 0
	end
end

local function create_table(player)
	local player_stats = {}

	for _, stat in pairs(player.leaderstats:GetChildren()) do
		player_stats[stat.Name] = stat.Value
	end

	return player_stats
end

local function onPlayerExit(player)  --Runs when players exit
	local player_stats = create_table(player)

	local success, err = pcall(function()
		local playerUserId = "Player_" .. player.UserId
		playerData:SetAsync(playerUserId, player_stats) --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)

lol, you thought the stat is named “money” but its actually “points”

It still worked tho. It’s just an instant.

1 Like

Also, This is the result when I tested your script. devforum

1 Like

I mean the “coins.Name” is “Points”. But the below instance shows this "coins.Value = data[‘Coins’]. In this case, the data[‘Coins’] doesn’t exist.

1 Like

No, no. When he saved he saved it as “Coins”
image
So when he gets the data he gets data[“Coins”] which is the same because it existed as this name.
image

image

1 Like
local function create_table(player)
	local player_stats = {}

	for _, stat in pairs(player.leaderstats:GetChildren()) do
		player_stats[stat.Name] = stat.Value
	end

	return player_stats
end

I guess the original function says anything.

1 Like

I meant THIS code.
https://devforum.roblox.com/t/datasave-is-not-working/1993441/8?u=tuliscool_yt

1 Like