Attempt to index nil with number (I am going to provide information)

I have a ban system, (reason, os.time, (only days), etc)

However, when fetching the player id and putting it into the ban datastore, I have ran into an error, the script looks a little something like this.

game.Players.PlayerAdded:Connect(function(plr) -- When a player joins the server
	local data = bandata:GetAsync(plr.UserId) -- Ban Data
	if data[1] == plr.UserId then
		if os.time() >= data[2] then
			bandata:SetAsync(plr.UserId,{".",".","."})
		else
-- rest of the script would go down here after the else statement, like reason, time, kick, etc

The issue is that on the 3rd line of the code I sent (if data[1] == plr.UserId) it fails to “index nil with number” Not quite sure what it means, but I do know that errors like that are usually typos or incorrect variables, so if you guys need more code, you can ask for it, I will send the entire script if that would help.

I’ve tried searching a bit online, I’m relatively new to scripting, I got most of this code from mixed sources (free models, posts, internet, etc) and pieced it together, while trying to understand what each variable does.

The weird thing is, the script functions fine, it works, I can ban people with reason and the time does work, so I don’t really understand what the issue is.
At first I thought maybe it was loading in too fast or something so I added a wait(1) line at the top, all that did was not save the time and make the banning system act as a kick.

I am happy to provide more information if needed.
Again I am relatively new to scripting, I am completely open to critiquing and information! I really appreciate things like this because it helps me grow into a better scripter!

Appreciate it guys! :+1:

1 Like

A player’s UserId is a number, so that entry in your datastore doesn’t exist!

1 Like

As Midnightific has mentioned, there doesn’t exist an entry for that player in the datastore. You’ll need to check if it exists beforehand

if not data then return end

^^ That will cause the function to stop if the data doesn’t exist.

If you need data to exist for each player always, you’ll need to set that yourself. E.g.

if not data then
    data = { whatever data you need to have here }
    bandata:SetAsync(plr.UserId, data)
end

Alternatively, you could use UpdateAsync.

bandata:UpdateAsync(plr.UserId, function(data)
    if not data then data = { whatever default stuff goes here } end
    ........
    return data
end)
1 Like

Thanks to both of you guys for the help! I do appreciate it.

Thanks to both of you for taking time to help explain what I need to do and what I could do as alternatives :heart:

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.