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

Why is this DataStore ban script throwing an error?

Asked by
Txeer 46
4 years ago

I am experimenting with a simple ban system. I am trying to save the admin name and a true/false value that says whether the player is banned or not. To do this, I am saving a table and it is not going so well. It throws this error when I try to index plrdata: ServerScriptService.BanWatch:12: attempt to index local 'plrdata' (a boolean value). I am not sure why it is doing this.

Script that watches for banned players to join:

local localdata = game:GetService("DataStoreService"):GetDataStore("bans")


game.Players.PlayerAdded:Connect(function(plr)
    local plrdata
    pcall(function()
        plrdata = localdata:GetAsync(plr.UserId.."-ban")
    end)

    if plrdata ~= nil then

        if plrdata['bankey'] == true then
            plr:Kick("You have been banned by the admin "..plrdata[1]..", you may appeal if eligible.")
        end

    else

        local banInfo = {"none", false}

        localdata:SetAsync(plr.UserId.."-ban", banInfo)
    end
end)

Script that sets the table:

game.ReplicatedStorage.RemoteEvents.Ban.OnServerEvent:Connect(function(plr, userid)

    local banInfo = {plr.Name, ['bankey'] = true}

    game:GetService("DataStoreService"):GetDataStore("bans"):SetAsync(userid.."-ban", banInfo)


    if game.Players:GetPlayerByUserId(userid) then
        game.Players:GetPlayerByUserId(userid):Kick("You have been banned by the admin "..plr.Name..".")
    end
end)
0
Have you tried this in an actual game and not studio? Datastores in studio dont work at the moment EnzoTDZ_YT 275 — 4y
0
@EnzTDZ_YT i dont think it would be the studios fault if the error is about the misinterpretation of "plrdata" being a bool speedyfox66 237 — 4y
0
Utilize pcall's return values (success, error?) to determine if plrdata was unable to be retrieved. If success is false, return false to the function. Otherwise, continue. That's the purpose of pcall. Fifkee 2017 — 4y
0
@Fifkee Yeah thats true here is some example : if success then print("Data successfully saved") smarsh1 0 — 4y

1 answer

Log in to vote
0
Answered by 4 years ago

So when you are creating the table baninfo. You said local banInfo = {plr.Name, [‘bankey’] = true }

You don’t have to have brackets for that. Just say

local banInfo = {Name = plr.Name, banKey = true }
0
Then when you call it use plrData[“banKey”] SethHeinzman 284 — 4y
Ad

Answer this question