So i want to save coins but im not sure how to, can someone help me?
when i try making the coins a seperate value in the same folder the “replicateddatafolder” it doesn’t save so i just deleted it. How do i put the coins value inside the “inventorystring” and then make it save?
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ServerStorage = game:GetService("ServerStorage")
local HttpService = game:GetService("HttpService")
local Players = game:GetService("Players")
local DataStore2 = require(ServerStorage:WaitForChild("DataStore2"))
game.Players.PlayerAdded:Connect(function(player)
local InventoryStore = DataStore2("Inventory", player)
local replicatedDataFolder = Instance.new("Folder")
replicatedDataFolder.Parent = ReplicatedStorage.ReplicatedData
replicatedDataFolder.Name = player.UserId
local inventoryString = Instance.new("StringValue")
inventoryString.Parent = replicatedDataFolder
inventoryString.Name = "Inventory"
local inventoryData = InventoryStore:Get({})
inventoryString.Value = HttpService:JSONEncode(inventoryData)
InventoryStore:OnUpdate(function(decodedData)
inventoryString.Value = HttpService:JSONEncode(decodedData)
end)
end)
Players.PlayerRemoving:Connect(function(player)
local ReplicatedData = ReplicatedStorage.ReplicatedData:FindFirstChild(player.UserId)
if (ReplicatedData) then
ReplicatedData:Destroy()
end
end)
Not sure if read about using DataStore2. First of all, this module automatically caches data so there’s no point in creating another place to display data. Secondly, for this module, the data is meant to be updated when something gets changed, this means saving the data when the player leaves is incorrect.
To access the data inside a client script, one possible way is to fire a remote to the client when OnUpdate fires. The local script will then recieve the updated data and save it as local cache inside the script itself or otherwise.
i already did a fire client on here a serverscript
local function updateClientCurrency(amount)
replicatedStorage.Events.UpdateClientCurrency:FireClient(player, amount)
end
updateClientCurrency(CurrencyStore:Get(DefaultCurrencyAmount))
CurrencyStore:OnUpdate(updateClientCurrency)