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

Do I use pcall() to safely save data in DataStore?

Asked by 5 years ago

So i am aware my script either saves too many values for the datastore to handle. How can I get a lot of values to save?

local DataStore = game:GetService("DataStoreService")
local pb = DataStore:GetDataStore("PistolBullets")
local pm = DataStore:GetDataStore("PistolMag")
local cps = DataStore:GetDataStore("Caps")
local bnty = DataStore:GetDataStore("Bounty")
local hp = DataStore:GetDataStore("Health")

game.Players.PlayerAdded:Connect(function(player)
    local pistolbullets = player:WaitForChild("pistolbullets")
    local pistolmag = player:WaitForChild("pistolmag")
    local caps = player:WaitForChild("caps")
    local bounty = player:WaitForChild("bounty")
    local health = player:WaitForChild("hp")

    pistolbullets.Value = pb:GetAsync(player.UserId) or 100
    pistolmag.Value = pm:GetAsync(player.UserId) or 0
    caps.Value = hp:GetAsync(player.UserId) or 0
    bounty.Value = cps:GetAsync(player.UserId) or 0
    health.Value = hp:GetAsync(player.UserId) or 100

        game.Players.PlayerRemoving:Connect(function(player)

            pb:SetAsync(player.UserId, pistolbullets.Value)
            pm:SetAsync(player.UserId, pistolmag.Value)
            cps:SetAsync(player.UserId, caps.Value)
            bnty:SetAsync(player.UserId, bounty.Value)
            hp:SetAsync(player.UserId, health.Value)

            if bounty.Value > 0 then
            bnty:SetAsync(player.UserId, bounty.Value)
        end
    end)
end)
2
better off using a dictionary and a single datastore rather than multiple datastores theking48989987 2147 — 5y
0
You can do that, but like what theking48989987 said, a single datastore would be more reasonable than multiple. Brayswaggy 14 — 5y
0
Yes, It's best to use pcall().it will help prevent the Script from causing further issues if the requested Data Retrieval fails Ziffixture 6913 — 5y

1 answer

Log in to vote
1
Answered by 5 years ago

As stated in the comments, it is best if you used a Dictionary (see the wiki) to store your data under one data store rather than several.

To answer your question on Pcalls, Roblox made data service retry by itself so handling a pcall to attempt seeing if the data store was capable of being accessed is not quite needed. However, if you fear passing an invalid value such as the wrong data type to your data stores you can use a pcall. In my opinion, there isn't much reason to use pcalls when saving and getting data because roblox handles the common reason for failure and other errors are more on fault of the developer but it may be best practice to use pcalls to keep data safe rather than corrupting a players data and ruining their ability to play.

Note: Roblox did disable the aforementioned feature for data service at one point, but I believe it has been enabled, I am not sure. Also sorry for any errors, I wrote this on my phone

Ad

Answer this question