Hello, I wrote this script to test saving any leaderstats value and unfortunately it will not save the 'Schmeckles' value. Is anybody experienced enough with DataStore that they could point out what I've done wrong? I left the script in Server Script Storage.
local DataStore = game:GetService("DataStoreService"):GetDataStore('Currency') game.Players.PlayerAdded:connect(function(player) local stats = Instance.new("IntValue", player) stats.Name = "leaderstats" local Schmeckles = Instance.new("IntValue", stats) Schmeckles.Name = "Schmeckles" local key = "player-"..player.userId local SavedValues = DataStore:GetAsync(key) if SavedValues then Schmeckles.Value = SavedValues[1] else DataStore:SetAsync(key, Schmeckles.Value) end end) game:BindToClose(function() game.Players.PlayerRemoving:connect(function(player) local key = "player-"..player.userId local Schmeckles = player.leaderstats.Schmeckles.Value DataStore:SetAsync(key, Schmeckles) end) end)
Try this? It waits 3 seconds to wait for the game to shut down if that doesn't work then tell me. Also waits for the data to load!
local DataStore = game:GetService("DataStoreService"):GetDataStore('Currency') local RunService = game:GetService('RunService') game.Players.PlayerAdded:connect(function(player) if not RunService:IsStudio() then Parent:WaitForDataReady() end local stats = Instance.new("IntValue", player) stats.Name = "leaderstats" local Schmeckles = Instance.new("IntValue", stats) Schmeckles.Name = "Schmeckles" local key = "player-"..player.userId local SavedValues = DataStore:GetAsync(key) if SavedValues then Schmeckles.Value = SavedValues[1] else DataStore:SetAsync(key, Schmeckles.Value) end end) game.Players.PlayerRemoving:connect(function(player) local key = "player-"..player.userId local Schmeckles = player.leaderstats.Schmeckles.Value DataStore:SetAsync(key, Schmeckles) end) game.OnClose = function() wait(3) end