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

[SOLVED] Multiple keys when using two DataStores in one script?

Asked by
zblox164 531 Moderation Voter
5 years ago
Edited 5 years ago

I am making a game using DataStoresand I want to use more than when DataStoreto save different things. For Example: Have one DataStorefor the players cash, xp, and level and one for the players health, walkspeed, and last CFrame (To load the players position when they join back on the game). My question is when using multiple DataStoresdo I need more than one key variable? Example:

local DataStoreService = game:GetService("DataStoreService")
local DataStoreCXL = DataStoreService:GetDataStore("CashXPLevel")
local DataStoreHWLC = DataStoreService:GetDataStore("HealthWalkSpeedLastCFrame")

local function Load(plr)
    local key = "plr-"..plr.UserId
    local key2 = "plr-"plr.UserId -- do I need this key?

    local Stats1
    local Stats2

    pcall(function()
        Stats1 = DataStoreCXL:GetAsync(key)
        Stats2 = DataStoreHWLC:GetAsync(key2)
    end)

    if Stats1 then
        -- Load data
    else
        -- More code
    end

    if Stats2 then
        -- Load data
    else
        -- More code
    end
end

local function Save(plr)
    local key = "plr-"..plr.UserId -- the first key
    local key2 = "plr-"..plr.UserId -- second key

    pcall(function()
        DataStoreCXL:SetAsync(key, SomeTable) -- Save the game stats
        DataStoreHWLC:SetAsync(key2, SomeOtherTable) -- Save the players stats
    end)    
end

game.Players.PlayerAdded:Connect(Load)
game.Players.PlayerRemoving:Connect(Save)

I know the example is not in a working state it was just to show my idea.

Thanks for helping!

0
You don't need to use separate data stores at all! You can save tables, the table will contain all values. User#19524 175 — 5y
0
But if I do want to use two data stores do I need another key? zblox164 531 — 5y
0
variable zblox164 531 — 5y
0
You should only need one key, of course you will still need Stats1 & Stats2 (the keys are referencing the same thing anyway) Vulkarin 581 — 5y
View all comments (4 more)
0
Thanks If you post that as a answer I will accept it! (: zblox164 531 — 5y
0
you mean me? Vulkarin 581 — 5y
0
Yes zblox164 531 — 5y
0
hmm okay Vulkarin 581 — 5y

1 answer

Log in to vote
1
Answered by
Vulkarin 581 Moderation Voter
5 years ago

You should only need one key, of course you will still need Stats1 & Stats2 (the keys are referencing the same thing anyway)

Ad

Answer this question