Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Why is this DataStore ban script throwing an error?

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)

4 comments

0

Have you tried this in an actual game and not studio? Datastores in studio dont work at the moment

0

@EnzTDZ_YT i dont think it would be the studios fault if the error is about the misinterpretation of "plrdata" being a bool

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.

0

@Fifkee Yeah thats true here is some example : if success then print("Data successfully saved")

1 answer

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 }

1 comment

0

Then when you call it use plrData[“banKey”]

This discussion is preserved as it was. New answers, comments, and votes are closed.