Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

script to save people amaont of coins when they leave it doesnt work please tell me what to fix?

local datastore = game:GetService("DataStoreService")
local ds2 = datastore:GetDataStore("CashSaveSystem")

game.Players.PlayerAdded:Connect(function(plr)
	local folder = Instance.new("Folder", plr)
	folder.Name = "leaderstats"
	local cash = Instance.new("IntValue", folder)
	cash.Name = "Cash"


	cash.Value = ds2:GetAsync(plr.UserId) or 0
	ds2:SetAsnyc(plr.UserId, cash.Value)

	cash.Changed:connect(function()
		ds2:SetAsync(plr.UserId, cash.Value)
	end)
end)

3 comments

0

Is this your entire code??

0

yes

0

Put all your code between a code block. How to post good questions/answers here:https://scriptinghelpers.org/help/how-post-good-questions-answers

1 answer

Here is my script I made once I made some notes to it so you can see what is needed

local players = game:GetService("Players") --Get players
local datastore = game:GetService("DataStoreService")  --Need to get the DataStoreService
local MoneyStore = datastore:GetDataStore("MoneyStore")---You need a data store to store in


--Creates Leaderstats folder
players.PlayerAdded:connect(function(player)
	local folder = Instance.new("Folder")
	folder.Name = "leaderstats"
	folder.Parent = player
	
--Create money
	local currency = Instance.new("IntValue")
	currency.Name = "Money"
	currency.Parent = player.leaderstats
	currency.Value = MoneyStore:GetAsync(player.UserId) or 0
	
	if currency.Value <= 0 then
		currency.Value = 200
	end

	MoneyStore:SetAsync(player.UserId, currency.Value) --Syncs Datastore with the money
	
--Save money when it changes
	currency.Changed:connect(function()
		MoneyStore:SetAsync(player.UserId, currency.Value)
		
		local data
		local success, errorMessage = pcall(function()
			data = MoneyStore:GetAsync(player.UrserId)
		end)		
		
	
		
	end)

end)

Also helps if you have something that makes sure it saves proply every time


--Saves money when player leaves game
game.Players.PlayerRemoving:Connect(function(player)
	
	local currency = player.leaderstats.Money
	MoneyStore:SetAsync(player.UserId, currency.Value)

	local data
	local success, errorMessage = pcall(function()    ---Pcalls helps if a player has a error
		data = MoneyStore:GetAsync(player.UrserId)
	end)		

end)

--Make sur there is no errors when the last player in the sever leaves
game:BindToClose(function(player)
	
	local currency = player.leaderstats.Money
	MoneyStore:SetAsync(player.UserId, currency.Value)

	local data
	local success, errorMessage = pcall(function()  ---Pcalls helps if a player has a error
		data = MoneyStore:GetAsync(player.UrserId)
	end)		


end)

PS you need to enable allow data stores in the roblox game settings, or roblox with block any thing from saving there.

For more see https://developer.roblox.com/en-us/articles/Data-store

This discussion is preserved as it was. New answers, comments, and votes are closed.