Money wont save

Hey there im trying to make a currency system when i add money to my player and rejoin it is the same as the starter one
the code will be bellow i have searched youtube the whole day to find a working one there was non please help
local currencyName = “coins”
local DataStore = game:GetService(“DataStoreService”):GetDataStore(“TestDataStore”)
game.Players.PlayerAdded:Connect(function(player)

local folder = Instance.new("Folder")
folder.Name = "leaderstats"
folder.Parent = player	

local currency = Instance.new("IntValue")
currency.Name = currencyName
currency.Parent = folder

local ID = currencyName.."-"..player.UserId
local savedData = nil	

pcall(function()
	savedData = DataStore:GetAsync(ID)
end)

if savedData ~= nil then
	currency.Value = savedData
	print("Data loaded")
else
	-- New player
	currency.Value = 5
	print("New player to the game")
end

end)

game.Players.PlayerRemoving:Connect(function(player)
local ID = currencyName…“-”…player.UserId
DataStore:SetAsync(ID,player.leaderstats[currencyName].Value)
end)

game:BindToClose(function()

-- When game is ready to shutdown

for i, player in pairs(game.Players:GetPlayers()) do
	if player then
		player:Kick("This game is shutting down")
	end
end

wait(5)	

end)

5 Likes

if you are testing this in studio, instead of just using player:Kick(), you should just save the Data of the player.

how do i do that i’m not the best at scripting

but when i leave and join back the data is lost

This should be the BindToClose function:

game:BindToClose(function()
	for i, player in pairs(game.Players:GetPlayers()) do
		if player then
			local ID = currencyName…"-"…player.UserId
			DataStore:SetAsync(ID, player.leaderstats[currencyName].Value)
		end
	end	
end)

im getting some errors

This means the problem is where you are calling the data.

ah what can i do about that is it something i cant fix?

It is. Just give me a bit to figure out the problem by testing it. Not sure what the problem is currently.

ok thats fine take your time :smiley:

Here you go:

local currencyName = "coins"
local DataStore = game:GetService("DataStoreService"):GetDataStore("TestDataStore")
game.Players.PlayerAdded:Connect(function(player)

	local folder = Instance.new("Folder")
	folder.Name = "leaderstats"
	folder.Parent = player	

	local currency = Instance.new("IntValue")
	currency.Name = currencyName
	currency.Parent = folder

	local ID = currencyName.."-"..player.UserId
	local savedData	

	pcall(function()
		savedData = DataStore:GetAsync(ID)
	end)

	if savedData then
		currency.Value = savedData
		print("Data loaded")
	else
		-- New player
		currency.Value = 5
		print("New player to the game")
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	local ID = currencyName.."-"..player.UserId
	DataStore:SetAsync(ID,player.leaderstats[currencyName].Value)
end)

game:BindToClose(function()
	for i, player in pairs(game.Players:GetPlayers()) do
		if player then
			local ID = currencyName.."-"..player.UserId
			DataStore:SetAsync(ID, player.leaderstats[currencyName].Value)
		end
	end	
end)

Seems the problem was either you were using the savedData variable as nil and checking whether it was still nil or because you had an unknown pair of quotation marks that didn’t work with Roblox scripting.

1 Like

ah thanks ill try out the fixed script now

nope still puts me back on 5 any ideas?

ah, forgot to test that :sweat_smile:

ah its fine i think its how it stores it idk

Here, the problem this time was the pcall function for some reason lol

local currencyName = "coins"
local DataStore = game:GetService("DataStoreService"):GetDataStore("TestDataStore")
game.Players.PlayerAdded:Connect(function(player)

	local folder = Instance.new("Folder")
	folder.Name = "leaderstats"
	folder.Parent = player	

	local currency = Instance.new("IntValue")
	currency.Name = currencyName
	currency.Parent = folder

	local ID = currencyName.."-"..player.UserId
	local savedData	= DataStore:GetAsync(ID)

	if savedData then
		currency.Value = savedData
		print("Data loaded")
	else
		-- New player
		currency.Value = 5
		print("New player to the game")
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	local ID = currencyName.."-"..player.UserId
	DataStore:SetAsync(ID,player.leaderstats[currencyName].Value)
end)

game:BindToClose(function()
	for i, player in pairs(game.Players:GetPlayers()) do
		if player then
			local ID = currencyName.."-"..player.UserId
			DataStore:SetAsync(ID, player.leaderstats[currencyName].Value)
		end
	end	
end)

This is when I start thinking the pcall function is deprecated.

1 Like

ah ill try out this one now lol

if it still doesn’t work its probably because the data is already saved try another data store name instead of "TestDataStore"

also GetAsync and SetAsync should be wrapped in a pcall

will do ill try this now script above now

still on 5 sigh ill see how to add a nother data store (i dont understand it)