I've tried looking online, youtube tutorials, etc. but none of the ones I found worked.
Here's one of the codes that I made, similar to the online ones.
local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("BucksStats")
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder") leaderstats.Name = "leaderstats" leaderstats.Parent = player
local Bucks = Instance.new("StringValue") Bucks.Name = "Bucks" Bucks.Parent = leaderstats Bucks.Value = DataStore:GetAsync(player.UserId) or 0
end)
game.Players.PlayerRemoving:Connect(function(player) DataStore:SetAsync(player.UserId, player.leaderstats.Bucks.Value) end)
I used a regular (not module or local script) in ServerScriptService. Tested it, did the command :change OmegaNuker Cash 2, then rejoined the game, but I then had 0 Cash. I was expecting it to be 2 cash, but it didn't work that way.
I see critical flaws with your data. First of all, you're not using pcall
. pcall
is designed to catch errors and is very useful for situations such as Datastores. Also, it is recommended (by roblox) that you use game:BindToClose()
, game:BindToClose()
allows you to run code. After 30 seconds, it will shut down, regardless of the code. (but it should be fast enough to save the player's data.)
local dataStoreService = game:GetService("DataStoreService") local dataStore = dataStoreService:GetDataStore("BucksStats") game.Players.PlayerAdded:Connect(function(player) local leaderstats = Instance.new("Folder") leaderstats.Name = "leaderstats leaderstats.Parent = player local bucks = Instance.new("IntValue") --If you're using numbers, it is recommend that you use an IntValue instead. bucks.Name = "Bucks" bucks.Parent = leaderstats --You need to parent it to leaderstats, not the player. local data --You should add it in order to get the data. default variables are always equal to nil. local success, errorMessage = pcall(function() --If you did not add pcall, it would break your script. data = dataStore:GetAsync(player.UserId) or 0 end) if success then --Alternative: If success == true print("Data found!") --You should add prints in order to troubleshoot. bucks.Value = data else warn("Error while getting data: "..errorMessage) --This gives some sort of code if there is any problem. end end) game.Players.PlayerRemoving:Connect(function(player) local success, errorMessage = pcall(function() dataStore:SetAsync(player.UserId, player.leaderstats.Bucks.Value) end) if success then print("Data saved and player left") --You should add some sort of different message, in order to not confuse it between others. else warn("Error while saving data: "..errorMessage) end end) game:BindToClose(function() for _, player in pairs(game.Players:GetPlayers()) do --This gets the player in the server. local success, errorMessage = pcall(function() dataStore:SetAsync(player.UserId,player.leaderstats.Bucks.Value) end) if success then print("Data saved and game closed") else warn("Error while saving data: "..errorMessage) end end end)