Ban wont detect player when rejoin?

When I tested the game (was not in studio) the ban did not work. It kicks you then when it checks it doesn’t?

local dss = game:GetService("DataStoreService")
local bds = dss:GetDataStore("banDataStore")

script.Parent.BanEvent.OnServerEvent:Connect(function(player, userName, reason)
	local success, errormessage = pcall(function()
		local plr = game.Players:FindFirstChild(userName)
		if plr then
			bds:SetAsync(plr.UserID, true)
		end
	end)
	warn(userName.." ".."was banned!")

	game.Players:WaitForChild(userName):Kick(reason)
end)
local banned
game.Players.PlayerAdded:Connect(function(PlayerName)
	local success, errormessage = pcall(function()
		banned = bds:SetAsync(PlayerName.UserId)
	end)
	if banned == true then
		PlayerName:Kick("Banned")
	end
end)

GetAsync should have been used, you used the wrong method.

Switched it and its still brocken?

local dss = game:GetService("DataStoreService")
local bds = dss:GetDataStore("banDataStore")

script.Parent.BanEvent.OnServerEvent:Connect(function(player, userName, reason)
	local success, errormessage = pcall(function()
		local plr = game.Players:FindFirstChild(userName)
		if plr then
			bds:SetAsync(plr.UserID, true)
		end
	end)
	warn(userName.." ".."was banned!")

	game.Players:WaitForChild(userName):Kick(reason)
end)
local banned
game.Players.PlayerAdded:Connect(function(PlayerName)
	local success, errormessage = pcall(function()
		banned = bds:GetAsync(PlayerName.UserId)
	end)
	if banned == true then
		PlayerName:Kick("Banned")
	end
end)

Some other issues include typos. On top of everything else(but much trivial), the pcall isn’t used correctly.

Analysis and some completion(and refactoring):

local DataStoreService = game:GetService("DataStoreService")
local Players = game:GetService("Players")

local banDataStore = DataStoreService:GetDataStore("banDataStore") -- lol

script.Parent.BanEvent.OnServerEvent:Connect(function(player, userName, reason)
	-- I'd advise checking if player is someone with powers to ban someone lol
	-- or else some random person with memory access could just ban someone for fun
	local plr = Players:FindFirstChild(userName)
	if not plr then
		return
	end

	--[[ If you want to stick to the usual method
	local success, response = pcall(function()
		banDataStore:SetAsync(plr.UserId, true)
	end)
	]]
	local success, response = pcall(banDataStore.SetAsync, banDataStore, plr.UserId, true)

	if success then
		warn(userName .. " was banned!")
		plr:Kick(reason)
	else
		warn(response)
	end
end)

Players.PlayerAdded:Connect(function(player)
	--[[ If you want to stick to usual method
	local success, banned = pcall(function()
		return banDataStore:SetAsync(player.UserId)
	end)
	]]
	local success, banned = pcall(banDataStore.GetAsync, banDataStore, player.UserId)
	if success then
		if banned then
			player:Kick("Banned")
		end
	else
		-- Handle something if data store somehow fails to GetAsync
		warn("Woops... something isn't right")
	end
end)

I’m very new to datastores and this helps me a lot thank you :smiley:

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