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

How to save multiple Bool (or anything other than string really) values in a Datastore?

Asked by 5 years ago

So yeah, as you can see from this script below, I really do not understand Datastore that well. I've got 2 other scripts that follow the same thing, but one uses numbers instead.

For what this is, these variables check if you have a piece of cosmetic. One of these gets checked to true after a chance when you win a game. What cosmetic you've equipped is in another script though that uses a similar structure to this one.

The error here is probably very obvious if you know how Datastores work. But this works well until a third player enters the server. When a third player enters, the datastore throttles (that's what i think it is?) and they end up not getting their variables correctly. I went quite far with this until I decided to test it.

local data = game:GetService("DataStoreService")
local date = data:GetDataStore("Verbas2")
local key = "play_"

game.Players.PlayerAdded:connect(function(plyr)
    local a=Instance.new("Folder")
    a.Parent=plyr
    a.Name="Hat"
        local stat1=Instance.new("BoolValue") -- every variable from here on is just to check if you have the item
        stat1.Value=date:GetAsync(key.."1"..plyr.UserId, stat1.Value)
        stat1.Parent=a
        stat1.Name="S1"
        local stat2=Instance.new("BoolValue")
        stat2.Value=date:GetAsync(key.."2"..plyr.UserId, stat2.Value)
        stat2.Parent=a
        stat2.Name="S2" 
        local stat3=Instance.new("BoolValue")
        stat3.Value=date:GetAsync(key.."3"..plyr.UserId, stat3.Value)
        stat3.Parent=a
        stat3.Name="S3"
        local stat4=Instance.new("BoolValue")
        stat4.Value=date:GetAsync(key.."4"..plyr.UserId, stat4.Value)
        stat4.Parent=a
        stat4.Name="H1"
        local stat5=Instance.new("BoolValue")
        stat5.Value=date:GetAsync(key.."5"..plyr.UserId, stat5.Value)
        stat5.Parent=a
        stat5.Name="F1"
        local stat6=Instance.new("BoolValue")
        stat6.Value=date:GetAsync(key.."6"..plyr.UserId, stat6.Value)
        stat6.Parent=a
        stat6.Name="T1"
        local stat7=Instance.new("BoolValue")
        stat7.Value=date:GetAsync(key.."7"..plyr.UserId, stat7.Value)
        stat7.Parent=a
        stat7.Name="HT1"
        local stat8=Instance.new("BoolValue")
        stat8.Value=date:GetAsync(key.."8"..plyr.UserId, stat8.Value)
        stat8.Parent=a
        stat8.Name="H2"
        local stat9=Instance.new("BoolValue")
        stat9.Value=date:GetAsync(key.."9"..plyr.UserId, stat9.Value)
        stat9.Parent=a
        stat9.Name="H3"
        local stat10=Instance.new("BoolValue")
        stat10.Value=date:GetAsync(key.."10"..plyr.UserId, stat10.Value)
        stat10.Parent=a
        stat10.Name="H4"
        local stat11=Instance.new("BoolValue")
        stat11.Value=date:GetAsync(key.."11"..plyr.UserId, stat11.Value)
        stat11.Parent=a
        stat11.Name="H5"
        local stat12=Instance.new("BoolValue")
        stat12.Value=date:GetAsync(key.."12"..plyr.UserId, stat12.Value)
        stat12.Parent=a
        stat12.Name="T2"
        local stat13=Instance.new("BoolValue")
        stat13.Value=date:GetAsync(key.."13"..plyr.UserId, stat13.Value)
        stat13.Parent=a
        stat13.Name="F2"
        local stat14=Instance.new("BoolValue")
        stat14.Value=date:GetAsync(key.."14"..plyr.UserId, stat14.Value)
        stat14.Parent=a
        stat14.Name="H6"
        local stat15=Instance.new("BoolValue")
        stat15.Value=date:GetAsync(key.."15"..plyr.UserId, stat15.Value)
        stat15.Parent=a
        stat15.Name="F3"
        local stat16=Instance.new("BoolValue")
        stat16.Value=date:GetAsync(key.."16"..plyr.UserId, stat16.Value)
        stat16.Parent=a
        stat16.Name="T3"
        local stat17=Instance.new("BoolValue")
        stat17.Value=date:GetAsync(key.."17"..plyr.UserId, stat17.Value)
        stat17.Parent=a
        stat17.Name="H7"
        local stat18=Instance.new("BoolValue")
        stat18.Value=date:GetAsync(key.."18"..plyr.UserId, stat18.Value)
        stat18.Parent=a
        stat18.Name="H8"
        local stat19=Instance.new("BoolValue")
        stat19.Value=date:GetAsync(key.."19"..plyr.UserId, stat19.Value)
        stat19.Parent=a
        stat19.Name="H9"
        local stat20=Instance.new("BoolValue")
        stat20.Value=date:GetAsync(key.."20"..plyr.UserId, stat20.Value)
        stat20.Parent=a
        stat20.Name="HT2"
        local stat21=Instance.new("BoolValue")
        stat21.Value=date:GetAsync(key.."21"..plyr.UserId, stat21.Value)
        stat21.Parent=a
        stat21.Name="H10"
end)

game.Players.PlayerRemoving:connect(function(plyr)
local a=plyr.Hat
wait(0.1)
    local stat= a.S1 -- saves all, or i wished it did
        stat.Value = date:SetAsync(key.."1"..plyr.UserId, stat.Value)
    local stat2= a.S2
        stat2.Value = date:SetAsync(key.."2"..plyr.UserId, stat2.Value)
    local stat3= a.S3
        stat3.Value = date:SetAsync(key.."3"..plyr.UserId, stat3.Value)
    local stat4= a.H1
        stat4.Value = date:SetAsync(key.."4"..plyr.UserId, stat4.Value)
    local stat5= a.F1
        stat5.Value = date:SetAsync(key.."5"..plyr.UserId, stat5.Value)
    local stat6= a.T1
        stat6.Value = date:SetAsync(key.."6"..plyr.UserId, stat6.Value)
    local stat7= a.HT1
        stat7.Value = date:SetAsync(key.."7"..plyr.UserId, stat7.Value)
    local stat8= a.H2
        stat8.Value = date:SetAsync(key.."8"..plyr.UserId, stat8.Value)
    local stat9= a.H3
        stat9.Value = date:SetAsync(key.."9"..plyr.UserId, stat9.Value)
    local stat10= a.H4
        stat10.Value = date:SetAsync(key.."10"..plyr.UserId, stat10.Value)
    local stat11= a.H5
        stat11.Value = date:SetAsync(key.."11"..plyr.UserId, stat11.Value)
    local stat12= a.T2
        stat12.Value = date:SetAsync(key.."12"..plyr.UserId, stat12.Value)
    local stat13= a.F2
        stat13.Value = date:SetAsync(key.."13"..plyr.UserId, stat13.Value)
    local stat14= a.H6
        stat14.Value = date:SetAsync(key.."14"..plyr.UserId, stat14.Value)
    local stat15= a.F3
        stat15.Value = date:SetAsync(key.."15"..plyr.UserId, stat15.Value)
    local stat16= a.T3
        stat16.Value = date:SetAsync(key.."16"..plyr.UserId, stat16.Value)
    local stat17= a.H7
        stat17.Value = date:SetAsync(key.."17"..plyr.UserId, stat17.Value)
    local stat18= a.H8
        stat18.Value = date:SetAsync(key.."18"..plyr.UserId, stat18.Value)
    local stat19= a.H9
        stat19.Value = date:SetAsync(key.."19"..plyr.UserId, stat19.Value)
    local stat20= a.HT2
        stat20.Value = date:SetAsync(key.."20"..plyr.UserId, stat20.Value)
    local stat21= a.H10
        stat21.Value = date:SetAsync(key.."21"..plyr.UserId, stat21.Value)
end)

If anyone could help me out with datastores and help me fix this mess I would really appreciate thanks!

0
well, i think you can save a table, but not sure idk much about ds starmaq 1290 — 5y

1 answer

Log in to vote
0
Answered by
zblox164 531 Moderation Voter
5 years ago
Edited 5 years ago

You should be able to save all the data you need in a table. Simply place all the data into a table and save the table. When you need it again then you can use the saved table to get the data back. From my understanding of your code this will not save any of the first values but only the last one because DataStore's over write data not save. When your set your data, all your really doing is changing the saved data. With a table it will greatly shorten your code and works fine.

Example:

local players = game:GetService("Players")
local dataStoreService = game:GetService("DataStoreService")
local dataStore = dataStoreService:GetDataStore("Example")

local function Save(plr)
    local save = {
        ["Stat1"] = stat1.Value,
        ["Stat2"] = stat2.Value,
        etc...
    }

    local key = plr.UserId

    local success, err = pcall(function()
        dataStore:SetAsync(key, save)
    end)

    if not success then
        warn("Failed to write data"..tostring(err))
        return
    end
end

local function Load(plr)
    local stats = Instance.new("Folder")
    stats.Name = "Hat"
    stats.Parent = plr

    local stat1 = Instance.new("BoolValue")
    stat1.Name = "1"
    stat1.Parent = stats

    local stat2 = Instance.new("BoolValue")
    stat1.Name = "2"
    stat1.Parent = stats

    local key = plr.UserId

    local saved

    local success, err = pcall(function()
        saved = dataStore:GetAsync(key)
    end)

    if not success then
        warn("Failed to read data"..tostring(err))
        return
    end

    if saved then
        stat1.Value = saved.Stat1
        stat2.Value = saved.Stat2

        etc...
    else
        Save(plr)
    end
end

players.PlayerAdded:Connect(Load)
players.PlayerRemoving:Connect(Save)

The code is just an example and will not work properly without editing.

If you need anymore information here are some links to some of the topics I mentioned:

Data Store Service and Tables and Dictionary's

Hope this helps!

0
Thank you! I’ll test this when I get on my computer again, but I’m sure it works properly since I’ve seen data constructed like this, but I didn’t understand it before. I’ll mark it when I’m on my computer. LUCABLA 7 — 5y
0
Ok! Hope it works! zblox164 531 — 5y
0
Okay so I tested it with 8 test players in 1 server and it loaded their data with no issues. Thank you so much for the script! Also I think you forgot to close the functions on the pcalls, but I fixed that. LUCABLA 7 — 5y
0
Ok great! Also I fixed the pcalls in the answer LOL! zblox164 531 — 5y
Ad

Answer this question