How To Check If A DataStore Has No Data Saved?

Now, I have a script that checks game bans. The only thing is, there’s not data saved on that DataStore and it keep saying that it’s an object not a table.

Not can I fix this?

local DSS = game:GetService("DataStoreService")
local Bans = DSS:GetDataStore("Bans")

return function()
	while wait(5) do
		print("Bans Checked")
		if Bans == nil then
			Bans:SetAsync("Main", {})
		end
		for _,v in pairs(game.Players:GetPlayers()) do
			if table.find(Bans, v.Name) then
				v:Kick("\n\nAdminSystems\n\nYou Are Banned From This Game!")
			end
		end
	end
end
1 Like

Bans is a GlobalDataStore instance, not a table. Did you mean to go through Bans:GetAsync("Main")?

Ah yes, sorry. It’s late at night so I can’t think straight. Thanks!

I also have this other script that doesn’t work.

local DSS = game:GetService("DataStoreService")
local Bans = DSS:GetDataStore("Bans")
local BanM = Bans:GetAsync("Main")

script.Parent.OnServerEvent:Connect(function(p, per)
	table.insert(BanM, per)
	Bans:SetAsync("Main", BanM)
end)

That means there is no data for the key "Main"

Ok, I have set it :smiley:

The only thing now is that it doesn’t kick me…