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

Basic explanation of how to use arrays in a datastore?

Asked by
rokedev 71
5 years ago

I've tried a bunch of ways, nothing really worked and i deleted most of it out of frustration. If i want to make a list of items in a array and communicate them to the client, how do i fire the array to client? Or, how do i update the array on the server by waiting for a remoteevent from the client?

1 answer

Log in to vote
1
Answered by 5 years ago

First things first you CANNOT use ordered datastores with arrays! Instead use a normal Datastore:

https://www.robloxdev.com/articles/Data-store

Next, I hope you are familiar with saving and getting data. Here is a simple script that saves and loads for you :)

local datastoreService = game:GetService("DataStoreService")
local datastore = datastoreService:GetDataStore("LeaderstatsSaveSystem")

--Load in data
game.Players.PlayerAdded:connect(function(plr)
    --Get stored data
    local MainSave = datastore:GetAsync(plr.UserId)
    --CHANGE THE FOLLOWING FOR YOUR LEADERSTATS
    local currencyName1 = plr.Leaderstats.currencyName1
    local currencyName2 = plr.Leaderstats.currencyName2
    local currencyName3 = plr.Leaderstats.currencyName3
    local currencyName4 = plr.Leaderstats.currencyName4
    if MainSave then --checks if the player has saved data
        currencyName1.Value = MainSave[1] --sets the value to the saved data
        currencyName2.Value = MainSave[2]
        currencyName3.Value = MainSave[3]
        currencyName4.Value = MainSave[4]
    end
end)

--Save on leave
game.Players.PlayerRemoving:Connect(function(plr)
    --CHANGE THE FOLLOWING FOR YOUR LEADERSTATS
    local currencyName1 = plr.Leaderstats.currencyName1
    local currencyName2 = plr.Leaderstats.currencyName2
    local currencyName3 = plr.Leaderstats.currencyName3
    local currencyName4 = plr.Leaderstats.currencyName4
    local Save = {currencyName1, currencyName2, currencyName3, currencyName4}
    datastore:SetAsync(plr.UserId, Save)
end)

You are Welcome :)

PS: If this works please accept as answer.

0
you can try using numeric for loops to shorten your code theking48989987 2147 — 5y
Ad

Answer this question