Data Save Not working?

local dss = game:GetService(“DataStoreService”)

local DataStoreToKeep = dss:GetDataStore(“TypeWhatYouWantHere”)

game.Players.PlayerAdded:Connect(function(player)
local is = Instance.new(“Folder”,player)
is.Name = “leaderstats”

local money = Instance.new("IntValue",is)
money.Name = "Money"
money.Value = 0

local data
local success, errormessage = pcall (function()
	data = DataStoreToKeep:GetAsync(player.UserId.."~money")
end)
if success then
	money.Value = data
else
	warn(errormessage)
end

end)

game.Players.PlayerRemoving:Connect(function(player)

local success, errormessage = pcall(function()
	DataStoreToKeep:SetAsync(player.UserId.."~money",player.leaderstats.Money.Value)
end)
if success then
else
	
	warn(errormessage)
end

end)

1 Like

Did it warn error message? Also Datastores often work poorly in the Studio. Have you tried testing in the actual game?

no i only tested in studio but in order to do what im trying to save cost 500 robux

Leaving Studio Is The Same As A Shutdown. It Uses A Function Known As “BindToClose”. For this to 100% Work, You Need To Go To Game Settings And Enable The Setting Called “Enable Studio Access To API Services”. I Suggest That You Completely Copy And Paste The Following:

local dss = game:GetService(“DataStoreService”)

local DataStoreToKeep = dss:GetDataStore(“TypeWhatYouWantHere”)

game.Players.PlayerRemoving:Connect(function(player)

local function Set(player)
local success, errormessage = pcall(function()
	DataStoreToKeep:SetAsync(player.UserId.."~money",player.leaderstats.Money.Value)
end)
if success then
else
	warn(errormessage)
end
end


game.Players.PlayerAdded:Connect(function(player)
local is = Instance.new(“Folder”,player)
is.Name = “leaderstats”

local money = Instance.new("IntValue",is)
money.Name = "Money"
money.Value = 0

local data
local success, errormessage = pcall (function()
	data = DataStoreToKeep:GetAsync(player.UserId.."~money")
end)
if success then
	money.Value = data
else
	warn(errormessage)
end
end)

game.Players.PlayerRemoving:Connect(Set)

game:BindToClose(function()
	for i, v in next, game.Players:GetChildren() do
		if v then
			Set(v)
		end
	end
end)
1 Like

Ok that worked but it removed money script off leader board. How do i recover it?

Shouldn’t be doing that. Maybe you changed something.

nah, all I did was copy and paste but dont worry ill figure out the problem.

You don’t need the “~money” string.
To get and save the data, you only need to do:

DataStoreToKeep:SetAsync(player.UserId,player.leaderstats.Money.Value) -- save
DataStoreToKeep:GetAsync(player.UserId) -- get
1 Like

so delete all of that and type this?

No, Its that you don’t need to type in “~money” in the getasync and setasync.

- i fixed it for you … use this .
*you are using and . you should use " and " .

local dss = game:GetService("DataStoreService")
local DataStoreToKeep = dss:GetDataStore("TypeWhatYouWantHere")

-- Get Player MONEY Data
game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	local money = Instance.new("IntValue")
	money.Name = "MONEY"
	money.Parent = leaderstats
	
	local data
	local success, errormessage = pcall(function()
		data = DataStoreToKeep:GetAsync(player.UserId.."_money")
	end)
	if success then
		player.leaderstats.MONEY.Value = data
	else
		warn(errormessage)
	end
end)

-- Save player MONEY Data When Leave The Server
game.Players.PlayerRemoving:Connect(function(player)
	local data
	local success, errormessage = pcall(function()
		data = DataStoreToKeep:SetAsync(player.UserId.."_money", player.leaderstats.MONEY.Value)
	end)
	if success then
		player.leaderstats.MONEY.Values = data
	else
		warn(errormessage)
	end
end)

:slight_smile: have a nice day .