Ban function fails to store if the player is banned in datastore

I am trying to make a function so you can input a player’s name and it will find that player id then it will see if the player who joins the game has that id and if it does it will keep kicking them until the datastore value is false. But the console keeps printing the “Failed to save ban state” line every time before the player gets kicked. Any reason why it won’t save?

Ban handler:

local dss = game:GetService("DataStoreService")
local BanDss = dss:GetDataStore("BannedDataStore")

game.ReplicatedStorage.Remotes.BanHandle.BanRequest.OnServerEvent:Connect(function(player, Textboxtext, Reasontext)
	
	local PlayerDefined = Textboxtext
	local char = game.Workspace:WaitForChild(PlayerDefined) -- GET PLAYER IN WORKSPACE
	--print(localplayer)
	local localplayer = game.Players:GetUserIdFromNameAsync(Textboxtext)
	print("THIS IS PRESENT PLAYER: " .. localplayer)
	local playerUserId = localplayer
	
	local isBanned = game.Workspace:WaitForChild(PlayerDefined).BanCheck -- Find ban bool
	print(isBanned.Name)
	
	local BanData 
	local success, errormessage = pcall(function()
		BanData = BanDss:GetAsync(playerUserId)
	end)

	if success then
		isBanned.Value = BanData
		print(BanData)
	end

	if success then
		print("Player is now TERMINATED from current session and all future joins")
	end
	isBanned.Value = true
	-- Save ban state
	local plr = player.Name
	local BanData = game.Workspace:WaitForChild(PlayerDefined).BanCheck.Value
	print(game.Workspace:WaitForChild(PlayerDefined).BanCheck.Value)
	local success, error = pcall(function()
		BanData:SetASync(playerUserId, BanData)
	end)

	if success then
		print("Banstate = " ..  game.Workspace(plr).BanCheck.Value)

	else
		print("Failed to save ban state")
		warn(error)
	end

Ban script to check the joining players:

local dss = game:GetService("DataStoreService")
local BanDss = dss:GetDataStore("BannedDataStore")
game.Players.PlayerAdded:Connect(function(Player)
	local localplr = Player.Name
	local isBanned = game.Workspace:WaitForChild(localplr):WaitForChild("BanCheck")
	print(isBanned.Value)
	
	print("Make sure isbanned is defined " .. isBanned.Name)
	local localplayer = game.Players:WaitForChild(Player):GetUserIdFromNameAsync()
	print("THIS IS PRESENT PLAYER: " .. localplayer)
	local playerUserId = localplayer
	local PlayerID = Player.UserId
	
	-- DATASTORE
	local BanData 
	local success, errormessage = pcall(function()
		BanData = BanDss:GetAsync(playerUserId)
	end)
	
	
	
	if success then
		isBanned.Value = BanData
	end
	if isBanned.Value == true then
		game.Players:GetPlayerFromCharacter(Player):Kick("You are banned!")
	end
end)


I am not good with datastores so any help appreciated.

You have SetASync (second S is capital) when it should be SetAsync. Heh.
image

2 Likes

Well, I changed it to that SetAsync and it still prints the same thing and doesn’t save it. But I’m sure that was one of the problems so thanks for pointing that out to me

I have found some new information about datastore and I think I might have to set something to the string but im not sure but it still won’t work. Here is updated code for the ban handler script

local dss = game:GetService("DataStoreService")
local BanDss = dss:GetDataStore("BannedDataStore")

game.ReplicatedStorage.Remotes.BanHandle.BanRequest.OnServerEvent:Connect(function(player, Textboxtext, Reasontext)
	local PlayerDefined = Textboxtext
	local char = game.Workspace:WaitForChild(PlayerDefined) -- GET PLAYER IN WORKSPACE
	local localplayer = game.Players:GetUserIdFromNameAsync(Textboxtext)
	print("THIS IS PRESENT PLAYER: " .. localplayer)
	local playerUserId = localplayer

	local isBanned = game.Workspace:WaitForChild(PlayerDefined).BanCheck -- Find ban bool
	print(isBanned.Name)
	
	game.Workspace:WaitForChild(PlayerDefined).BanCheck.Value = true
	--Load data
	local BanData 
	local success, errormessage = pcall(function()
		BanData = BanDss:GetAsync(playerUserId)
	end)
	
	
	if success then
		isBanned.Value = BanData
		print(BanData)
	end

	local plr = PlayerDefined
	
	game.Workspace:WaitForChild(PlayerDefined).BanCheck.Value = true
	
	-- Save ban state
	local BanData = game.Workspace:WaitForChild(PlayerDefined).BanCheck.Value
	print(game.Workspace:WaitForChild(PlayerDefined).BanCheck.Value)
	
	local success, errormessage = pcall(function()
		BanData:SetAsync(playerUserId, (tostring(BanData)))
	end)
	
	
	if success then
		--print("Banstate = " ..  game.Workspace(plr).BanCheck.Value)
		print("Player has been kicked from current session and has been banned from all future joins")
	else
		print("Failed to save ban state")
		print("Player ban request was unsuccessful..")
		warn(errormessage)
	end
	
	game.Players:GetPlayerFromCharacter(char):Kick("You are banned for: " .. Reasontext )
end)





















--banbutton.MouseButton1Click:Connect(function(hit)
	--if hit.Parent:FindFirstChild("Humanoid") then
	--	local char = hit.Parent
	--	local player = game.Players:GetPlayerFromCharacter(char)
	--	local playerUserId = player.UserId

	--	local success, errormessage = pcall(function()
	--		banDataStore:SetAsync(playerUserId, true)		
	--	end)

	--	if success then
	---		print("Player is now TERMINATED from current session and all future joins")
	--	end

	--	player:Kick("TERMINATION NOTICE: You were banned and kicked for an unknown reason")
--	end
--end)

but I keep getting this error from the warning function: " ServerScriptService.Datastore.BanHandle.BanHandle:36: attempt to index boolean with ‘SetAsync’" Anyone know how to fix this?

local success, errormessage = pcall(function()
		BanDss:SetAsync(playerUserId, (tostring(BanData)))
	end)

When you did set async you did it on a boolean which was banData so change it to this and it should be fine hopefully

Thank you! I do not know why I did not realize this before but thank you for pointing this out

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.