Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

My datastore doesn't work! It's buggy and doesn't remember the correct value! How do I fix it?

Asked by
Fako16 13
6 years ago

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)
0
stop using this code, it isn't great, has no pcall to back it up, can cause lots of datastore losses... User#20388 0 — 6y
0
I think for now no pcall is fine since pcall doesn't let you see the error.About the question can you add more specific details. PlaasBoer 275 — 6y
0
I think the leaderstats is supposed to be a model, based on other scripts. s1muIate -35 — 6y

1 answer

Log in to vote
0
Answered by
s1muIate -35
6 years ago

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!

0
The entire script was by Reshiram110 so go check him out on ROBLOX! https://www.roblox.com/users/73223877/profile s1muIate -35 — 6y
Ad

Answer this question