Hello, I am attempting to save data in my game but the code is not working. I'm not sure of the problem and I'm pretty sure many others are having the same problem. My code is below. ~~~~~~~~~~~~~~~~~ local DataStoreService = game:GetService("DataStoreService") local CoinsDataStore = DataStoreService:GetDataStore("CoinsDataStore") game.Players.PlayerAdded:Connect(function(player) local Folder = Instance.new("Folder",player) Folder.Name = "leaderstats" Folder.Parent = player local Samples = Instance.new("IntValue",Folder) Samples.Name = "Samples"
local coins = Instance.new("IntValue",Folder)
coins.Name = "Coins"
coins.Parent = Folder
local playerUserId = "Player"..player.UserId
--Load Data
local data
local success,errormessage = pcall(function()
data = CoinsDataStore:GetAsync(playerUserId)
end)
if success then
if data then
coins.Value = data.coins
Samples.Value = data.Samples
end
-- Set data == current values
end
end)
game.Players.PlayerRemoving:Connect(function(player) local playerUserId = "Player_"..player.UserId local data = { coins = player.leaderstats.Coins.Value, samples = player.leaderstats.Samples.Value } local success, errormessage = pcall(function() CoinsDataStore:SetAsync(playerUserId, data) end) if success then print("Data successfully saved!") else print("Error saving data! If you see this do not leave the game or your save data will be deleted! Stay here while the problem is resolved!") warn(errormessage) end end)
--All of that is the code idk why it is not displaying it all in the code part.

1 comment
you have to make sure the "~" things are either completely below or completely above the code in order to fit it into the format