Money wont save

ok so i should have 69 coins image image 0

Do you have any other data-storing script that may be interfering with the values?

I will put this basic Instance based data store that I’m currently using, if someone wants to learn from it.

-- DataStore.lua
-- @author Nanamin
-- January 2021

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local DataStoreService = game:GetService("DataStoreService")
local RunService = game:GetService("RunService")
local Players = game:GetService("Players")

local DataTemplate = require(script.DataTemplate) -- a table with default data

local DataStore = DataStoreService:GetDataStore("PlayerData")

local function createData(folder, data)
	for name,value in pairs(data) do
		if type(value) == "table" then
			local subFolder = Instance.new("Folder")
			subFolder.Name = name
			subFolder.Parent = folder
			createData(subFolder, value) -- this is a recursive call
		elseif type(value) == "number" then
			local NumberValue = Instance.new("NumberValue")
			NumberValue.Name = name
			NumberValue.Value = value
			NumberValue.Parent = folder
		elseif type(value) == "string" then
			local StringValue = Instance.new("StringValue")
			StringValue.Name = name
			StringValue.Value = value
			StringValue.Parent = folder
		elseif type(value) == "boolean" then
			local BoolValue = Instance.new("BoolValue")
			BoolValue.Name = name
			BoolValue.Value = value
			BoolValue.Parent = folder
		end
	end
end

local function load(player)
	local userId = player.UserId
	local attempts = 0
	local ok, result
	
	while not ok and attempts < 6 do
		ok, result = pcall(DataStore.GetAsync, DataStore, userId)
		if not ok then
			attempts += 1
			warn(result)
			wait(1)
		end
	end
	
	if ok then
		local folder = Instance.new("Folder")
		folder.Name = player.Name
		folder.Parent = ReplicatedStorage
		
		if result then
			createData(folder, result)
		else
			createData(folder, DataTemplate)
		end
	else
		player:Kick("unable to load data")
	end
end

local function save(player)
	if RunService:IsStudio() then
		return
	end
	
	local folder = ReplicatedStorage:WaitForChild(player.Name)
	
	local userId = player.UserId
	local attempts = 0
	local ok, result
	
	local saveTable = {}
	
	for _,item in ipairs(folder:GetChildren()) do
		if item:IsA("Folder") then
			for _,child in ipairs(item:GetChildren()) do
				saveTable[item.Name][child.Name] = child.Value
			end
		else
			saveTable[item.Name] = item.Value
		end
	end
	
	while not ok and attempts < 6 do
		ok, result = pcall(DataStore.SetAsync, DataStore, userId, saveTable)
		if not ok then
			attempts += 1
			warn(result)
			wait(7)
		end
	end
	
	if not ok then
		warn(result)
	end
end

Players.PlayerAdded:Connect(load)
Players.PlayerRemoving:Connect(save)

for _,player in ipairs(Players:GetPlayers()) do -- sometimes PlayerAdded event won't be called if there's too much information to process above
	coroutine.wrap(load)(player)
end

game:BindToClose(function()
	if not RunService:IsStudio() then
		for _,player in ipairs(Players:GetPlayers()) do
			coroutine.wrap(save)(player)
		end
	end
end)

i made a brand new game image

should i use that?:thinking: .

It’s up to you but I do recommend to use it since it provides data-store retries however this Instance based DataStore does not have session locking which means if you want to save tools you may get item duplication issues, that’s why I recommend using ProfileService.

ah right do i have to put the script on a local script?

Data-Store calls will only work in ServerScripts.

ok so thats right hmmm if you want i can show you the whole script

like the problems im haveing ingame

Which data store code are you currently using? the first code I provided should work fine when loading and saving data.

At the BindToClose function, save the user’s data. That might be why.

game.Players.PlayerRemoving:Connect(function(player)
local Coins = player.leaderstats.Coins
local ok, result = pcall(DataStore.SetAsync, DataStore, player.UserId, Coins.Value)

if ok then -- if we successfully saved the data
	warn("data has been saved")

Yes as MrAz19485 said it is necessary to use BindToClose to save data in studio because the PlayerRemoving event often fails but you can also destroy the player object through server perspective and it will instantly save.

this one?
game:BindToClose(function()
for _,player in ipairs(game.Players:GetPlayers()) do
coroutine.wrap(function()
local Coins = player.leaderstats.Coins
pcall(DataStore.SetAsync, DataStore, player.UserId, Coins.Value) – we don’t need to check there because its pointless (not in studio).
end)
end
end)

1 Like

I pretty sure this code should load and save: Money wont save - #36 by LiamKyd

i have that in the game and it is not functioning watch image image

bruh sometimes scripting makes me go crazyyyy, try that other script then I am leaving now.

what if that one doesnt work? confusion

because it doesnt even have a leader stat