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

How to save tables with datastore?

Asked by 6 years ago
Edited 6 years ago

Making a ban function which is connected to a table but I don't know how to.

The Module Script

local banned = {} 

reason = "You have been banned from the game from a Developer"

local datastore = game:GetService('DataStoreService'):GetDataStore('MHARebornBanList') --The DataStore 
local banlist = {}--Storing the Banlist 
function banned.AddBanlist(userid)
    table.insert(#banlist, userid)
    local key = "Banned-"..userid
    local getsave = datastore:GetAsync(key)
    if getsave then

    end
end
return banned
1
why are you doing table.insert(#t, e)? it should be table.insert(t, e) abnotaddable 920 — 6y
0
Why put this in a module? More work to require it. hiimgoodpack 2009 — 6y

1 answer

Log in to vote
1
Answered by
Goulstem 8144 Badge of Merit Moderation Voter Administrator Community Moderator
6 years ago
Edited 6 years ago

For your use, I think instead of saving a consistent table, you should save separate keys to a Banlist datastore.

You have to use the SetAsync function to set the player's ID into a new key.

--Establish datastore
local datastore = game:GetService("DataStoreService"):GetDataStore("BanList")
local module = {}; --Declare module's environment

function module.AddBanlist(userid)
    local data = datastore:GetAsync(tostring(userid))
    if not data then --If they're not already there
        datastore:SetAsync(tostring(userid),true) --Add them
    end
end

--Usage
module.AddBanlist(69913657);
1
+1, but you're not saving any requests by performing GetAsync - a blind SetAsync would work just as well. Recommended addition: function module.IsBanned(userid) return datastore:GetAsync(tostring(userid)) == true end Also, the module could be improved with error checking (since both Get/SetAsync can error) chess123mate 5873 — 6y
0
It prevents line 8 from firing if the datastore already contains the user. Goulstem 8144 — 6y
0
But you're right, it's not exactly saving any requests seeing as it has to call GetAsync to decide whether or not to call SetAsync. Goulstem 8144 — 6y
Ad

Answer this question