DataStore saving table issue

Hi,
Recently, I was to make a ban system by using DataStore, but when I check if the player has the table that contains if the player banned or not and the reason for the ban, it returns an error tells me that it doesn’t exist.
Error1:image

Another weird thing, when I try to unban a player that already banned and has the table, it gives me the same error that tells me that there is nothing called “banned”
Error2:
image

CODE:

local repStorage = game:GetService("ReplicatedStorage")
local dataStoreService = game:GetService("DataStoreService")
local players = game:GetService("Players")

local banRemote = repStorage:WaitForChild("BanRemote")
local banRemoteReturn = repStorage:WaitForChild("BanRemoteReturn")

local banList = dataStoreService:GetDataStore("BanList")

local function kickPlayer(plyr, msg)
if players:FindFirstChild(plyr) then
	print(plyr)
	print(plyr.ClassName)
	players[plyr]:Kick(msg)
end
end


players.PlayerAdded:Connect(function(plr)
local playerId = plr.UserId
local plyerStatues

local success, errorMassage = pcall(function()
	plyerStatues = banList:GetAsync(playerId)
end)

if plyerStatues.banned then
	
	print(plyerStatues.banned, plyerStatues.reason.."reason")
	if plyerStatues.banned == true then --Error1
		plr:Kick(plyerStatues.reason)
	end
	
end

end)


banRemote.OnServerEvent:Connect(function(plr, intendedPlr, command, massage, bantime)
print(command)
print(typeof(command))
if command == "Ban" then
	print("Called Ban")
	local playerId = players:GetUserIdFromNameAsync(intendedPlr)
	local setBan
	local plyerStatues
	local alreadyBanned
	
	local success, errorMassage = pcall(function()
		plyerStatues = banList:GetAsync(playerId)
		alreadyBanned = plyerStatues.banned
	end)
	
	if alreadyBanned == true then
		banRemoteReturn:FireClient(plr, command,"Already Banned")
		return
	end
	
	local good, errorMassage = pcall(function()
		setBan = banList:SetAsync(playerId, {banned = true; reason = massage;})
	end)
	
	kickPlayer(intendedPlr, "You have been banned for "..massage)
	
	banRemoteReturn:FireClient(plr, command,"Successfully Banned")
	return
	
end

if command == "Kick" then
	print("Called Kick")
	kickPlayer(intendedPlr, "You were kicked for "..massage)
	banRemoteReturn:FireClient(plr, command,"Successfully Kicked")
	return
end

if command == "Unban" then
	print("Called Unban")
	local playerId = players:GetUserIdFromNameAsync(intendedPlr)
	local plyerStatues
	local alreadyBanned
	
	local success, errorMassage = pcall(function()
		plyerStatues = banList:GetAsync(playerId)
	end)
	
	
	if plyerStatues.banned == true then --Error2
		plyerStatues.banned = false
		banRemoteReturn:FireClient(plr, command,"Successfully Unbanned")
		return
	else
		banRemoteReturn:FireClient(plr, command,"Not Banned")
		return
	end
	
end

end)
1 Like

There were a lot of things wrong with this code, mostly redundant variables, longer if statements, and your datastore was almost fine, you just forgot to add some checks with your datastores. Also, you were returning a lot when you didn’t need to (??). Read up on returning and when to use them when you can, cause in your code it would just be returning out of the entire function where it was called from, causing later code not to run

Also a tip for the future, for if statements, if what you’re checking for is a bool, instead of having to write this:

local testvar = true

if testvar == true then
    print("Hi")
end

You could write this instead

local testvar = true

if testvar then
    print("Hi")
end

Try this

local repStorage = game:GetService("ReplicatedStorage")
local dataStoreService = game:GetService("DataStoreService")
local players = game:GetService("Players")

local banRemote = repStorage:WaitForChild("BanRemote")
local banRemoteReturn = repStorage:WaitForChild("BanRemoteReturn")

local banList = dataStoreService:GetDataStore("BanList")

local function kickPlayer(plyr, msg)
	if players:FindFirstChild(plyr) then
		print(plyr)
		print(plyr.ClassName)
		plyr:Kick(msg)
	end
end


players.PlayerAdded:Connect(function(plr)
	local playerId = plr.UserId
	local plyerStatues
	
	local success, errorMassage = pcall(function()
		plyerStatues = banList:GetAsync(playerId)
	end)
	
	if success and plyerStatues then
		if plyerStatues.banned then
			print(plyerStatues.banned, plyerStatues.reason.."reason")
			plr:Kick(plyerStatues.reason)
		end	
	end
end)


banRemote.OnServerEvent:Connect(function(plr, intendedPlr, command, massage, bantime)
	print(command)
	print(typeof(command))
	if command == "Ban" then
		print("Called Ban")
		local playerId = players:GetUserIdFromNameAsync(intendedPlr)
		local setBan
		local plyerStatues
		local alreadyBanned
	
		local success, errorMassage = pcall(function()
			plyerStatues = banList:GetAsync(playerId)
		end)
		
		
		if success and plyerStatues then
			if plyerStatues.banned then
				banRemoteReturn:FireClient(plr, command, "Already Banned")
			end
		end
		
	
		local good, errorMassage = pcall(function()
			banList:SetAsync(playerId, {
				banned = true;
				reason = massage;
			})
		end)
		
		if good then
			print("Successfully saved ban info")
		end
		
		kickPlayer(intendedPlr, "You have been banned for "..massage)
	
		banRemoteReturn:FireClient(plr, command, "Successfully Banned")
	
	end

	if command == "Kick" then
		print("Called Kick")
		kickPlayer(intendedPlr, "You were kicked for "..massage)
		banRemoteReturn:FireClient(plr, command, "Successfully Kicked")
	end

	if command == "Unban" then
		print("Called Unban")
		local playerId = players:GetUserIdFromNameAsync(intendedPlr)
		local plyerStatues
		local alreadyBanned
	
		local success, errorMassage = pcall(function()
			plyerStatues = banList:GetAsync(playerId)
		end)
	
		if success and plyerStatues then
			if plyerStatues.banned then 
				plyerStatues.banned = false
				banRemoteReturn:FireClient(plr, command, "Successfully Unbanned")
			else
				banRemoteReturn:FireClient(plr, command, "Not Banned")
			end	
		end
	end
end)
3 Likes

Thank you much. You just missed a something too but it’s ok. What you missed is that set the new data when you unban the player.

if command == "Unban" then
	print("Called Unban")
	local playerId = players:GetUserIdFromNameAsync(intendedPlr)
	local plyerStatues
	local alreadyBanned
	
	local success, errorMassage = pcall(function()
		plyerStatues = banList:GetAsync(playerId)
	end)
	
	if success and plyerStatues then
		if plyerStatues.banned then
			
			local good, errorMassage = pcall(function()
				banList:SetAsync(playerId, {
					banned = false;
					reason = "";
				})
			end)
			
			banRemoteReturn:FireClient(plr, command,"Successfully Unbanned")
			
		else
			banRemoteReturn:FireClient(plr, command,"Not Banned")
			
		end
1 Like

Oh yeah, my bad. Anyways you’re welcome!!

1 Like