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

Unsure with how to go about using Datastore?

Asked by 4 years ago

Hey, Im making a simulator sort of game, and want to try to use datastore to do this. When I test this program it doesn't seem to save it (also in workshop in console it reads "18:51:39.812 - DataStore request was added to queue. If request queue fills, further requests will be dropped. Try sending fewer requests.Key = 84828376 (x2)")

I followed a yt tutorial fyi

local Players = game:GetService("Players")
local DataStore = game:GetService("DataStoreService")
local ds1 = DataStore:GetDataStore("PlayerMoneySave")
local ds2 = DataStore:GetDataStore("PlayerPopulationSave")
local ds3 = DataStore:GetDataStore("PlayerSoulsSave")


Players.PlayerAdded:Connect(function(player)
    --Create the folder to store the data
    local Statistics = Instance.new("Folder", player)
    Statistics.Name = "Statistics"

    --Create the data holders   
    local Money = Instance.new("IntValue", Statistics)  
    local Population = Instance.new("IntValue", Statistics) 
    local Resources = Instance.new("Folder", Statistics)    
    local Souls = Instance.new("IntValue", Statistics)

    --Renames the data holders
    Money.Name = "Money"
    Resources.Name = "Resources"
    Population.Name = "Population"
    Souls.Name = "Souls"



    --Money Data Sync

    Money.Value = ds1:GetAsync(player.UserId) or 1000
    ds1:SetAsync(player.UserId, Money.Value)

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

    -------------------------

    --Population Data Sync

    Population.Value = ds2:GetAsync(player.UserId) or 0
    ds2:SetAsync(player.UserId, Population.Value)

    Population.Changed:connect(function()
    ds2:SetAsync(player.UserId, Population.Value)
    end)

    -------------------------

    --Souls Data Sync

    Souls.Value = ds3:GetAsync(player.UserId) or 5
    ds3:SetAsync(player.UserId, Souls.Value)

    Souls.Changed:connect(function()
    ds3:SetAsync(player.UserId, Souls.Value)
    end)

end)


Players.PlayerRemoving:Connect(function(player)
    ds1:SetAsync(player.UserId, player.Statistics.Money.Value)
    ds2:SetAsync(player.UserId, player.Statistics.Population.Value)
    ds3:SetAsync(player.UserId, player.Statistics.Souls.Value)
end)
0
Research DataStore2 services. It's easier to use and more reliable. It's stat loss prevention and it cache's data until the player leaves and then it saves so it doesn't lag as much. It's really useful. If you want I can answer it with a script for your curiosity if you're wondering. killerbrenden 1537 — 4y
0
If you would be able to, that would be handy! Thanks PoppyandNeivaarecute 134 — 4y

1 answer

Log in to vote
0
Answered by 4 years ago

You should not be using multiple datastores for multiple values for a player unless it's explicitly needed, which often is not the case, and which I doubt it is in your case.

(To answer your questions exactly: You are sending too many save requests, you should only save periodically, and not when a value changes, especially if it's changing often.)

Instead, use a single datastore for the data, and update it when it's needed (periodically and when they leave). This results in less "set" operations to the database. Doing this, you can save all of the player data in a single table, instead of splitting it into multiple, individual tables!

So, to create an example of how to organize your players' data. (I'm keeping the code as simple as possible):

local Players = game:GetService("Players");
local DataStoreService = game:GetService("DataStoreService");
local PlayerDataStore = DataStoreService:GetDataStore("playerData");
local PlayersData = {};

Players.PlayerAdded:Connect(function (newPlayer)
    -- Fetches the data from datastore, or sets the default data for them
    local playerData = PlayerDataStore:GetAsync(newPlayer.UserId) or {
        money = 0,
        population = 0,
        souls = 0
    };
    -- Sets the player's data, so it's accessible by other functions in your code
    PlayersData[newPlayer.UserId] = playerData;
    print("Player", newPlayer.Name, "has", playerData.money, "money!");
end)

Players.PlayerRemoving:Connect(function (player)
    -- Fetch the player's data from the PlayersData table, and save it
    local playerData = PlayersData[player.UserId];

    PlayerDataStore:SetAsync(player.UserId, playerData)

    print("Saved player's data!");
end)
0
Doesnt work for some reason. It prints all the things in console but doesn't load it or save it PoppyandNeivaarecute 134 — 4y
Ad

Answer this question