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 7 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:

game.Players.PlayerAdded:connect(function(player)
    print("Loading banData for " .. player.UserId)

    local dataStoreService = game:GetService("DataStoreService")
    local data = dataStoreService:GetDataStore("banData")

    local banData = data:GetAsync(player.UserId .. "Data")

    print("banData loaded for " .. player.UserId)

    if not banData[1] == "" then
        print("Player " .. player.UserId .. "attemped to join the game, but was kicked.")
        print("Reason: 'You have been banned by " .. banData[1] .. "for " .. banData[2] .. ". Please PM " .. banData[1] .. " to appeal the ban.'")
        player.Kick("You have been banned by " .. banData[1] .. "for " .. banData[2] .. ". Please PM " .. banData[1] .. " to appeal the ban.")
    end
end)

The data should look like this

DATA
    > banData
        - 119226470Data
          -{"AlphaGamer150", "Testing"}

1 answer

Log in to vote
1
Answered by 7 years ago
Edited 7 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:

--This comment represents line 10 and before of your code
if  banData then --Using if (object) will evaluate if it exists or not
    --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 — 7y
Ad

Answer this question