Saving money (datastore)

Well, scripts update every time you join the game so you tested it right. You could also try and publish the game, then join the game and maybe refresh the script? (By publishing I mean, updating the game.)

Sorry, it still isn’t saving… I dont know what to do :confused:

You’re doing something very very very “stupid” about it.

This is how your script should be, instead:

local moneyDataStore = game:GetService("DataStoreService"):GetDataStore("Money")

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

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

	local playerUserId = "player_"..player.UserId
	--lOADING moni DATA
	local MoneyData
	local success, errormessage = pcall(function()
		MoneyData = moneyDataStore:GetAsync(playerUserId)
	end)

	if success then
		Money.Value = MoneyData
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	moneyDataStore:GetAsync(player.UserId,{
		player.leaderstats.money.Value,
	})
end)

Sorry… still doesn’t work !!!

moneyDataStore:GetAsync(player.UserId,{

He is not using any tables, and instead of GetAsync it should be set as SetAsync, and MoneyValue would be grabbing the table, and not the value meaning that you would have to put a [1] after calling :GetAsync() (for example: :GetAsync(playersid)[1] ) on user’s id. Still, great explanaition.

Okay, I found the issue, I forgot to add tostring()

local Data = game:GetService("DataStoreService"):GetDataStore("TheData")

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

	local Money = Instance.new("NumberValue", leaderstats)
	Money.Name = "money"
	Money.Value = 0
end)

game.Players.PlayerRemoving:Connect(function(player)
	Data:GetAsync(tostring(player.UserId),{
		player.leaderstats.money.Value,
	})
end)

Sorry, it still doesn’t work for some reason

DSS = game:GetService("DataStoreService")
Store = DSS:GetDataStore("Money")

Key = "Player_" -- Key
Storage = {} -- For Storing Data

Template = { -- The Items the Data will Save
   Cash = 100;
}
game.Players.PlayerAdded:Connect(function(p)
local Success, Data = pcall(function() -- pcall
  return Store:GetAsync(Key..p.UserId) -- returns data
end)

if Success then -- If Accessed DataStores
   if Data then -- If Data found
   Storage[p.UserId] = Data
   else -- if Player has no Data
   Storage[p.UserId] = Template
   end
else -- If Failed to Access DataStores
-- maybe retry or kick the player?
end
end)

game.Players.PlayerRemoving:Connect(function(p)
local Success = pcall(function()
  Store:SetAsync(Key..p.UserId, Storage[p.UserId]) -- saves Data
end)
if Success then -- if Successfully saved Data

else -- If Failed to Save

end

Storage[p.UserId] = nil -- in case Player rejoins
end)

Sorry, am i supposed to put it below the actual leaderstat script?

Great idea, but I would rather using MoneyValue.Value = (any...)

You actually aren’t supposed to put that below the leaderstat script, because it’s an idea where no value is being changed. If you are a scripter, you could try using a tutorial for it.

game.Players.PlayerAdded:Connect(function(p)
local leaderstats = Instance.new("Folder", player)
	leaderstats.Name = "leaderstats"
	
	local Money = Instance.new("IntValue", leaderstats)
	Money.Name = "money"
	Money.Value = 0

--[[ Previous Script
local Success, Data = pcall(function() -- pcall
  return Store:GetAsync(Key..p.UserId) -- returns data
end)

if Success then -- If Accessed DataStores
   if Data then -- If Data found
   Storage[p.UserId] = Data
   else -- if Player has no Data
   Storage[p.UserId] = Template
   end
else -- If Failed to Access DataStores
-- maybe retry or kick the player?
end
--]]
Money.Value = Storage[p.UserId]["Cash"] -- Value will equal to Table data
end)


(woops accidental reply)

But, it will only set the money value only on the current server, and when the player rejoins the player would not get any saved money, instead would be left with 0 money. (into other server)

Hi, I tried this but I think it only sets the money to 100, not sure why. I tried changing the value and rejoining but It got set to 100 again.

Are you sure you have Access to DataStores?

if this is what you mean, then yes.
image

Yes, thats how DataStores work.

This shouldnt happen

There was no single datastore call. It was kept without a datastore. If you would use that script, then it would only save on one server and not others. If it was using a datastore, then it would be saving in different servers.

Are you sure your script is inside ServerScriptService?

Please reread my previous my Previous post, that you are saying shouldnt happen here

DataStoreService will Save the Data of a Table, when the Player joins, it will get that Table and apply it,