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

My Data Store Isn't Saving And I am Not Sure Why?

My Data Store Isn't Saving At All And I Am Not Sure Why I Need Some Help

Here Is My Script

CashStore = game:GetService("DataStoreService"):GetDataStore("RPCash")
local plr = game.Players.PlayerAdded
local Money = script.Parent.Cash

function PlayerEntered(player)
	repeat wait() until player.Character
	
	local Money = script.Parent.Cash
	
	if CashStore:GetAsync("RPCash_"..player.Name) ~= nil then
		Money.Value = CashStore:GetAsync("RPCash_"..player.Name)
	else
		Money.Value = 500
	end
	
	Money.Changed:connect(function(Val)
		CashStore:SetAsync("RPCash_"..plr.Name, Val)
	end)

end

game.Players.PlayerAdded:connect(PlayerEntered)

game.Players.PlayerRemoving:Connect(function(plr)
	local Val = Money.Value
	
	local success, errormessage = pcall(function()
		CashStore:SetAsync("RPCash_"..plr.Name, Val)
	end)
	
	if success then
		wait()
	else
		print(errormessage)
		CashStore:SetAsync("RPCash_"..plr.Name, Val)
	end
end) 

1 comment

0

This is not the answer to your question, but you should never save data when the value changes. That is an easy way to hit the data store limits. And you are not even implementing data stores correctly.

1 answer

Create a Money value in the player when player joins. Get the Money value when the player leaves and save it.

CashStore = game:GetService("DataStoreService"):GetDataStore("RPCash")
local plr = game.Players.PlayerAdded
function PlayerEntered(player)
	repeat wait() until player.Character
	local Money = Instance.new("NumberValue", player)
	Money.Value = CashStore:GetAsync("RPCash_"..player.Name) or 500
	Money.Changed:connect(function(Val)
		CashStore:SetAsync("RPCash_"..plr.Name, Val)
	end)
end

game.Players.PlayerAdded:connect(PlayerEntered)

game.Players.PlayerRemoving:Connect(function(plr)
	local Money = plr.Money.Value
	local success, errormessage = pcall(function()
		CashStore:SetAsync("RPCash_"..plr.Name, Money)
	end)
	if success then
		wait()
	else
		print(errormessage)
		CashStore:SetAsync("RPCash_"..plr.Name, Money)
	end
end) 

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