Attempt to call a nil value, saving an array on a Datastore

  1. What do you want to achieve? I want to save an array to my datastore so that I can ban people from my game. Right now I am having a bug where when nobody is banned, I try to save an array to the datastore it throws an error

  2. What is the issue? For some reason saving an array to my datastore does not work. It says that I am trying to call a nil value

  3. What solutions have you tried so far? I have searched throughout Google and the Devforums and have followed the developer article

Here are the developer console errors

Here is some of my code (This Code is Run Every Time someone Joins the game)

local DataStoreService = game:GetService("DataStoreService")
local RunService = game:GetService("RunService")

if not RunService:IsStudio() then
	print("BannedStore Created")
	local BannedStore = DataStoreService:GetDataStore("PlayersBanned")
end

---- \/ Code That Runs When Player Joins \/ ----

local repeatTimes = 0
	
print("About to Run")
	
if not RunService:IsStudio() then
	local BansList
	
	repeat wait(.1)
		repeatTimes += 1
		local success, err = pcall(function() -- Checks Banned Datastore
			BansList = Banned:GetAsync("Bans")
		end)
			
		if not BansList then
				
			print(Banned)
				
			repeat wait(.1) -- Gets Stuck in This Loop
				print("Relooping")
				local success1, err1 = pcall(function()
					Banned:SetAsync("Bans", {2646833 --[[UserID's Here]]}) -- Error is here
				end)
	
				if success1 then
					print("Success!")
				else
					print(err1)
				end
			until success1
			print(BansList)
		end
	 
		if success then
			if table.find(BansList, UserID) then
				player:Kick("You have been banned from this game! If this is a mistake please contact the game creator.")
			end
		end
	until success or repeatTimes >= 10
	if repeatTimes >= 10 then player:Kick("Gameknight Commands- Error Code: 100, Please Rejoin!") warn(tostring(player).."'s Datastore Failed!") end
end

NOTE: I am running this code NOT on studio.

(Its probably something easy that I’m overlooking, thank you for helping :smiley: In advance)

Not sure if this will solve it but :SetAsync() is key, value. It seems “Bans” is not a key. Your script is just going overwrite the user’s ID to the key “Bans”. You should probably save ban data for each player individually instead of having all bans in a table.

1 Like

Yes, that was what I was going to do at first but I could not figure out how to tell if the player had data in the datastore saved yet :confused:

1 Like

Right, you can just do this:

local success, errormsg = pcall(function()
    BansList = Banned:GetAsync(--[[your key]])
end
if success then
    if success ~= nil then -- if the data exists
        if banned == true then -- You can just have a single value determining whether the player is banned. You can do it another way though.
            player:Kick()
        else -- if the data doesn't exist but no error is outputted
            --do stuff
        end
    end
else
    warn("Error retrieving ban data: "..errormsg
end
1 Like

Thank you :smiley: this looks like it should work! I’ll implement it and see if it works!

1 Like

oh my goodness i’m so dumb :sweat: I just realized I had my variables wrong for my datastore (Banned instead of BannedStore) Wow I have to keep better track of my variable. Anyway thank you for helping me :slight_smile:

1 Like

No worries. Feel free to reach out if you need further assistance.

1 Like