Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How to save tables with datastore?

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

2 comments

1

why are you doing table.insert(#t, e)? it should be table.insert(t, e)

0

Why put this in a module? More work to require it.

1 answer

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);

3 comments

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)

0

It prevents line 8 from firing if the datastore already contains the user.

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.

This discussion is preserved as it was. New answers, comments, and votes are closed.