Help with basic datastores

I want this to save, but every time I create new value and leave the game the values don’t save.


local dss = game:GetService("DataStoreService")
local Players = game:GetService("Players")
local ds = dss:GetDataStore("ds")

local stats = { 
	{"Cash", 30000};
	{"Xp", 50000};
}

local function leaderstats(player)
	local folder = Instance.new("Folder")
	folder.Name = "leaderstats"
	folder.Parent = player 
	
	
	for i,v in pairs(stats) do 
		local name = v[1]
		local value = v[2]
		local statValue = Instance.new("IntValue")
		statValue.Name = name 
		statValue.Value = value
		statValue.Parent = folder 
	end
end

local function loadPlayer(player)
	leaderstats(player)
	local data 
	local success, err = pcall(function()
		local data = ds:GetAsync(player.UserId)
	end)
	if success then 
		print("data loaded")
		player.leaderstats.Cash.Value = data
	else 
		print("data failed to load")
	end
end

local function savePlayer(player)
	local success, err = pcall(function()
		ds:SetAsync(player.UserId, player.leaderstats.Cash)
	end)
	if success then 
		print("data successfully saved")
	end
end


Players.PlayerAdded:Connect(loadPlayer)

Do you have API services on? What does it show on your output?

1 Like

Studio is incredibly buggy for saving datastores, I advise using an Open Client to test

1 Like

You’re trying to save the IntValue instance, not its value. player.leaderstats.Cash.Value is what you want. You might also need to use tostring(player.UserId) as the key.

maybe what @SSSpencer413 said too

2 Likes

I’ve noticed you never actually call savePlayer(). Is that possibly the reason?

(Also what @bytechan said - you can’t save an instance only a value)

Perhaps you meant to do:

Players.PlayerRemoving:Connect(savePlayer)
2 Likes

it doesn’t seem to be working even with the new code


local dss = game:GetService("DataStoreService")
local Players = game:GetService("Players")
local ds = dss:GetDataStore("ds")

local stats = { 
	{"Cash", 30000};
	{"Xp", 50000};
}

local function leaderstats(player)
	local folder = Instance.new("Folder")
	folder.Name = "leaderstats"
	folder.Parent = player 
	
	
	for i,v in pairs(stats) do 
		local name = v[1]
		local value = v[2]
		local statValue = Instance.new("IntValue")
		statValue.Name = name 
		statValue.Value = value
		statValue.Parent = folder 
	end
end

local function loadPlayer(player)
	leaderstats(player)
	local data 
	local success, err = pcall(function()
		local data = ds:GetAsync(tostring(player.UserId))
	end)
	if success then 
		print("data loaded")
		player.leaderstats.Cash.Value = data
	else 
		print("data failed to load")
	end
end

local function savePlayer(player)
	local success, err = pcall(function()
		ds:SetAsync(tostring(player.UserId), player.leaderstats.Cash.Value)
	end)
	if success then 
		print("data successfully saved")
	end
end


Players.PlayerAdded:Connect(loadPlayer)
Players.PlayerRemoving:Connect(savePlayer)

image

Test on an open client, like I said:

I wouldn’t advise using local here. You’ve already defined the data variable before. You should instead try assigning it.

yeah i did that and when im in game my cash is 0 while I made it so its 30000

You’re probably making it through Client Side Are you sure about this?

I think there is a semi recent post about this in #resources:community-tutorials.