I'm using a datastore to save my stats like Wood, etc.. You get wood by clicking on trees, but when I test this, I get for example 3 wood, but when I enter the game back I only have 2 wood? I lose random ammounts of my stats randomly, but my datastore seems to work fine, please help!
This is the data-store script.
local DataStore = game:GetService("DataStoreService") local ds = DataStore:GetDataStore("WoodSaveSystem") game.Players.PlayerAdded:connect(function(player) local stats = Instance.new("Folder",player) stats.Name = "Stats" local Wood = Instance.new("IntValue", stats) Wood.Name = "Wood" Wood.Value = ds:GetAsync(player.UserId) or 0 ds:SetAsync(player.UserId, Wood.Value) Wood.Changed:connect(function() ds:SetAsync(player.UserId, Wood.Value) end) end) game.Players.PlayerRemoving:connect(function(player) ds:SetAsync(player.UserId, player.Stats.Wood.Value) end)
You can get help here... but this does NOT overall resolve your problem. I have worked with scripts like these but they can be buggy if you don't pay attention.
--[[ A player has joined the game. Load/Create their data. --]] game.Players.PlayerAdded:connect(function(Player)
local PlayerData; --Will hold the player's data when fetched from the datastore. --[[ Set up the player's leaderstats and add them to the ROBLOX leaderboard. --]] local Stats=Instance.new('Model') Stats.Name="leaderstats" local Money=Instance.new('NumberValue',Stats) Money.Name="Money" Stats.Parent=Player --[[ We are fetching the player's data, if it exists. If it does not exist, we will create new data for the player as they must logically be new to the game. --]] local CanSave=Instance.new('BoolValue',Player) --This is used to determine whether or not the --player's data can be saved when they leave. CanSave.Name="CanSaveData" CanSave.Value=true local DataFetchSuccess,ErrorMessage=pcall(function() --We are wrapping the datastore request in a protected function, as web requests can error. --Safely handling datastore errors can prevent player data corruption. PlayerData=DataStore:GetAsync(tostring(Player.UserId)) end) if DataFetchSuccess then --The datastore GET request was successful! if PlayerData~=nil then --The player's data exists, they have played this game before. Put their data into the leaderboard. Player.leaderstats.Money.Value=PlayerData else --The player's data does not exist, they're new to the game. Create their data on the leaderboard. Player.leaderstats.Money.Value=100 --Players start out with 100 money in this example. end else --The GET request failed, datastores could be down. --[[ There are many ways to handle datastore failures. In this case, we will kick the player, letting them know their data failed to load. Because their data wasn't loaded, we won't save their data when they leave. ]]-- Player.CanSaveData.Value=false Player:Kick("Your data failed to load! Please rejoin.") end
end)
--[[ A player is leaving the game. Save their data. --]] game.Players.PlayerRemoving:connect(function(Player)
if Player.CanSaveData.Value==false then return end --Player data can't be saved, so do nothing. local PlayerData=Player.leaderstats.Money.Value local DataWriteSuccess,ErrorMessage=pcall(function() --Once again, we are safely calling a web request. If it fails, we can safely handle it. DataStore:SetAsync(tostring(Player.UserId),PlayerData) end) if not DataWriteSuccess then --Uh oh, player data didnt' save. Handle this error. --We will attempt to save the player's data 5 more times. If it fails after 5 times, --we will abort the save and the player's data will not be changed in the datastores. local Retry_Count=0 while Retry_Count<6 do wait(60) --Wait 1 minute between each retry local Succeded,Error=pcall(function() DataStore:SetAsync(tostring(Player.UserId),PlayerData) end) if Succeded then break end --HURRAY, DATA SAVED! Retry_Count=Retry_Count+1 end end
end)
also credit to Reshiram110 for the script!