I've made a datastore script to save rebirths, coins and gems. I get no errors (yey) but it just doesnt save (not yey). I have API services turned on and am testing it via roblox.com not studio. I must be doing smth wrong eh? cheers for the help if any.
local DataStoreService = game:GetService("DataStoreService") local playerData = DataStoreService:GetDataStore("PlayerData") local function onPlayerJoin(Player) local leaderstats = Instance.new("Folder", Player) leaderstats.Name = "leaderstats" local val1 = Instance.new("StringValue",Player) val1.Name = 'GotPet' val1.Value = '' local val2 = Instance.new("StringValue",Player) val2.Name = 'OpenValue' val2.Value = '' local Gems = Instance.new("IntValue", leaderstats) Gems.Name = "Gems" Gems.Value = 0 local Rebirths = Instance.new("IntValue", leaderstats) Rebirths.Name = "Rebirths" Rebirths.Value = 0 local Coins = Instance.new("IntValue", leaderstats) Coins.Name = "Coins" Coins.Value = 0 local playerUserId = "Player_" .. Player.UserId --Gets player ID local data = playerData:GetAsync(playerUserId) --Checks if player has stored data if data then print("Data found!") -- Data exists for this player Coins.Value = data Rebirths.Value = data Gems.Value = data else print("DataStore working but No data found") -- Data store is working, but no current data for this player Coins.Value = 0 Rebirths.Value = 0 Gems.Value = 0 end end local function onPlayerExit(player) --Runs when players exit local success, error = pcall(function() print("Data loaded successfully") local playerUserId = "Player_" .. player.UserId playerData:SetAsync(playerUserId, player.leaderstats.Coins.Value) playerData:SetAsync(playerUserId, player.leaderstats.Rebirths.Value) playerData:SetAsync(playerUserId, player.leaderstats.Gems.Value)--Saves player data end) if not success then warn('Could not save data!') end end game.Players.PlayerAdded:Connect(onPlayerJoin) game.Players.PlayerRemoving:Connect(onPlayerExit)
The way you save is wrong. It's gonna overwhelm the DatastoreService at times, and it's also gonna OVERWRITE everything that was set as the new one. (Like what you did, you saved Coins, but in the next line, you overwrite Coins with Rebirths.) It should be a table if you have multiple values.
And also the way that you load it is also wrong, like I said, using tables is best if you have multiple values to load.
Also, you should ALWAYS use pcall()
if you're operating with Datastores. Only with the Async, though.
Here is my fix: (Untested)
local DataStoreService = game:GetService("DataStoreService") local playerData = DataStoreService:GetDataStore("PlayerData") local function onPlayerJoin(Player) local leaderstats = Instance.new("Folder", Player) leaderstats.Name = "leaderstats" local val1 = Instance.new("StringValue",Player) val1.Name = 'GotPet' val1.Value = '' local val2 = Instance.new("StringValue",Player) val2.Name = 'OpenValue' val2.Value = '' local Gems = Instance.new("IntValue", leaderstats) Gems.Name = "Gems" Gems.Value = 0 local Rebirths = Instance.new("IntValue", leaderstats) Rebirths.Name = "Rebirths" Rebirths.Value = 0 local Coins = Instance.new("IntValue", leaderstats) Coins.Name = "Coins" Coins.Value = 0 local playerUserId = "Player_" .. Player.UserId --Gets player ID local Success, Result = pcall(function() return playerData:GetAsync(playerUserId) end) if Result then print("Data found!") Coins.Value = Result[1] -- // Since in the saving part, Coins are the one that is set as the first value of the table, we set Coins as [1] Rebirths.Value = Result[2] Gems.Value = Result[3] else print("DataStore working but No data found") warn(Result) end end local function onPlayerExit(player) --Runs when players exit local success, Err = pcall(function() print("Data Saved successfully") local playerUserId = "Player_" .. player.UserId playerData:SetAsync(playerUserId, {player.leaderstats.Coins.Value, player.leaderstats.Rebirths.Value, player.leaderstats.Gems.Value}) -- Now set as a table instead of individually end) if not success then warn(Err) warn('Could not save data!') end end game.Players.PlayerAdded:Connect(onPlayerJoin) game.Players.PlayerRemoving:Connect(onPlayerExit)