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

Why wont a DataStore array work?

Asked by 8 years ago

I am working on a ban script, anyway... upon starting the game it gives me this error: ServerScriptService.Script:11: attempt to index local 'banData' (a nil value)

Here is the script:

01game.Players.PlayerAdded:connect(function(player)
02    print("Loading banData for " .. player.UserId)
03 
04    local dataStoreService = game:GetService("DataStoreService")
05    local data = dataStoreService:GetDataStore("banData")
06 
07    local banData = data:GetAsync(player.UserId .. "Data")
08 
09    print("banData loaded for " .. player.UserId)
10 
11    if not banData[1] == "" then
12        print("Player " .. player.UserId .. "attemped to join the game, but was kicked.")
13        print("Reason: 'You have been banned by " .. banData[1] .. "for " .. banData[2] .. ". Please PM " .. banData[1] .. " to appeal the ban.'")
14        player.Kick("You have been banned by " .. banData[1] .. "for " .. banData[2] .. ". Please PM " .. banData[1] .. " to appeal the ban.")
15    end
16end)

The data should look like this

1DATA
2    > banData
3        - 119226470Data
4          -{"AlphaGamer150", "Testing"}

1 answer

Log in to vote
1
Answered by 8 years ago
Edited 8 years ago

First time visitors (or anyone who's coming on that isn't banned) to your place won't have anything in banData, so as a result attempting to index it by using banData[1] will result in an error. What you should really do is use the following:

1--This comment represents line 10 and before of your code
2if  banData then --Using if (object) will evaluate if it exists or not
3    --Continue from here
0
Thanks for the help, but @kingdom5 has already helped me, but since you gave the same answer he did, I accepted your answer! AlphaGamer150 101 — 8y
Ad

Answer this question