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

Can a DataStore Expert take a look at our Saving Scripts?

Asked by 5 years ago
Edited 5 years ago

This topic is all about saving and DataStores on Roblox. So if you are a DataStore expert this is your question.

So it seems like we are having a problem in our game where sometimes players are losing certain or all tools they purchase, the next time they load the game. Usually, all saving is working fine, just some rare times this happens, people have messaged me, and I assume we have been getting thumbs down due to this issue.

Just a brief: Our game has many tools and values all saving using DataStores, the values seem to be saving fine, it is the tools that I get most complaints about, people losing an item they really cared about or sometimes all items.

Could someone please take a look at our Tool Saving Script and tell us if they see anything wrong with it?

Tool Saving Script Link: Pastebin Link

This may also be a key to the mystery, but we do get a lot of warnings with our saving when it comes to values, for example. If someone goes to a shop and sells an item and gets some gold, we usually always get this warning. ("Request was throttled. Try sending fewer requests.")

Here is the code for how we save our values: Pastebin Link

Could the Gold.Changed:connect function in that script be causing the issue? What if the moment someone sells something and we get the Request Throttled warning, someone else leaves and causes the issue?

Let me know what you guys think, thanks in advance!

3 answers

Log in to vote
0
Answered by 5 years ago

In both of your scripts you have no error checking at all. If one of your getAsync or setAsync functions fails your players will loose some form of data.

Secondly never save data in a changed event you have no control over when it runs. DataStores have a limit for all request types and there is a set limit per key. Currently this is 6 seconds.

You first need to take a look at the following:-

  • per place limits
  • error handling

which are all on the post http://robloxdev.com/articles/Data-store

I also reccomend that you do not use game instances for making the saved data us a table which can be kept up to date and simply save.

As I do not know much about how your game is made I cannot say what is a better way to save your player data. But you should review why you use two data stores and how often you want to be saving the players data.

0
Thank you for your answer! peacepartystudios 9 — 5y
Ad
Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

this is not an answer i am just posting here so it's easier to read

1st script

local Data = game:GetService("DataStoreService"):GetDataStore("Inventory")
game.Players.PlayerAdded:Connect(function(plr)
    local SavedStuff = Data:GetAsync(plr.userId)
    if SavedStuff ~= nil then
        for i,v in pairs(SavedStuff)do

            if game.ServerStorage.Item_Category_Example_1:FindFirstChild(v) ~= nil then
                local Weapon = game.ServerStorage.Item_Category_Example_1:FindFirstChild(v):Clone()
                Weapon.Parent = plr:WaitForChild("Backpack")
            end

            if game.ServerStorage.Item_Category_Example_2:FindFirstChild(v) ~= nil then
                local Weapon = game.ServerStorage.Item_Category_Example_2:FindFirstChild(v):Clone()
                Weapon.Parent = plr:WaitForChild("Backpack")
            end

            if game.ServerStorage.Item_Category_Example_3:FindFirstChild(v) ~= nil then
                local Weapon = game.ServerStorage.Item_Category_Example_3:FindFirstChild(v):Clone()
                Weapon.Parent = plr:WaitForChild("Backpack")
            end

        end
    end
    plr.CharacterRemoving:Connect(function()
        plr.Character.Humanoid:UnequipTools()
    end)
end)
game.Players.PlayerRemoving:Connect(function(plr)
    local Table = {}
    for i,v in pairs(plr.Backpack:GetChildren())do
        table.insert(Table,v.Name)
    end
    if Table ~= nil then
        Data:SetAsync(plr.userId,Table)
    end
end)

2nd script

local DataStore = game:GetService("DataStoreService")
local ds1 = DataStore:GetDataStore("GoldSaveSystem")
?
game.Players.PlayerAdded:connect(function(player)
?
 local leader = player:WaitForChild("leaderstats")
 local Gold = Instance.new("IntValue",leader)
 Gold.Name = "Gold"
 Gold.Value = ds1:GetAsync(player.UserId) or 0
 ds1:SetAsync(player.UserId, Gold.Value)

 Gold.Changed:connect(function()
  ds1:SetAsync(player.UserId, Gold.Value)
 end)

end)
?
game.Players.PlayerRemoving:connect(function(player)
 ds1:SetAsync(player.UserId, player.leaderstats.Gold.Value)
end)
Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

Calling SetAsync every time someone's gold is changed is not ideal. You should either save their stuff everytime they leave and when the server shuts down(if possible). You should call GetAsync inside a pcall, because if you don't, the script might fail and the player might lose all their progress, then they might collect items and the script will save that, while removing all their past progress. And, do NOT save their progress if they don't have anything. I also saw in your first script that in the PlayerRemoving function you only checked inside the player's backpack, and not the Character. You should also check inside the Character in case someone leaves the game with a tool in their hand.

Try using these scripts, I'm pretty sure they're going to work.

Tool Saving Script:

local Data = game:GetService("DataStoreService"):GetDataStore("Inventory")
game.Players.PlayerAdded:Connect(function(plr)
    local success,message = pcall(function()
    local SavedStuff = Data:GetAsync(plr.UserId)
    end)
    if not success then
        plr:Kick("Error loading your data. Try again later.")
    end
    if SavedStuff ~= nil then
        for i,v in pairs(SavedStuff)do

            if game.ServerStorage.Item_Category_Example_1:FindFirstChild(v) ~= nil then
                local Weapon = game.ServerStorage.Item_Category_Example_1:FindFirstChild(v):Clone()
                Weapon.Parent = plr:WaitForChild("Backpack")
            end

            if game.ServerStorage.Item_Category_Example_2:FindFirstChild(v) ~= nil then
                local Weapon = game.ServerStorage.Item_Category_Example_2:FindFirstChild(v):Clone()
                Weapon.Parent = plr:WaitForChild("Backpack")
            end

            if game.ServerStorage.Item_Category_Example_3:FindFirstChild(v) ~= nil then
                local Weapon = game.ServerStorage.Item_Category_Example_3:FindFirstChild(v):Clone()
                Weapon.Parent = plr:WaitForChild("Backpack")
            end

        end
    end
    plr.CharacterRemoving:Connect(function()
        plr.Character.Humanoid:UnequipTools()
    end)
end)
game.Players.PlayerRemoving:Connect(function(plr)
    local Table = {}
    for i,v in pairs(plr.Backpack:GetChildren())do
        table.insert(Table,v.Name)
    end
for i,v in ipairs(plr.Character:GetChildren())do
        table.insert(Table,v.Name)
    end
    if Table ~= nil then
        Data:SetAsync(plr.UserId,Table)
    end
end)

Gold Saving Script:

local DataStore = game:GetService("DataStoreService")
local ds1 = DataStore:GetDataStore("GoldSaveSystem")
game.Players.PlayerAdded:connect(function(player)
 local leader = player:WaitForChild("leaderstats")
 local Gold = Instance.new("IntValue",leader)
 Gold.Name = "Gold"
local success,message = pcall(function()
Gold.Value = ds1:GetAsync(player.UserId) 
end)
if not success then
player:Kick("Error loading your data. Try again later.")
end
end)
game.Players.PlayerRemoving:connect(function(player)
if player.leaderstats.Gold.Value ~= 0 then
  ds1:SetAsync(player.UserId, player.leaderstats.Gold.Value)
end
end)
0
There are also question marks in the gold saving script, I removed them all. brokenVectors 525 — 5y
0
Never kick player if the data fails to load once thats just stupid User#5423 17 — 5y
0
^ then how do i prevent the player from wasting their time and resetting their data? brokenVectors 525 — 5y
0
by having a second cooldown before a retry with a max of 3. User#5423 17 — 5y
0
Oh god, I already used this strategy to try to save my table, it fails real bad greatneil80 2647 — 5y

Answer this question