Help fixing ban script

Hey everyone!
I am making an admin panel and need some help fixing this ban script. It kicks the player and provides a reason, but for some reason it does not ban the player and they can rejoin.

The system consists of a Script and LocalScript connected with a RemoteEvent.

LocalScript:

local box = script.Parent.Parent.TextBox
local box2 = box.Parent.Reason
local event = script.Parent.Parent.Parent.BanEvent
local title = box.Parent.TextLabel
local players = game:GetService("Players")
local text = nil
local reason = nil

box.FocusLost:Connect(function()
	text = box.ContentText
end)

box2.FocusLost:Connect(function()
	reason = box2.ContentText
end)

script.Parent.MouseButton1Click:Connect(function()
	if players:FindFirstChild(text) then
		if reason then
			event:FireServer(text, reason)
		elseif reason == nil then
			reason = "No reason provided"
			event:FireServer(text, reason)
		end
	elseif players:FindFirstChild(text) == nil then
		title.Text = "Player not found!"	
		wait(1)
		title.Text = "Ban Player"	
	end
end)

Script:

local event = script.Parent.Parent.Parent.BanEvent
local players = game:GetService("Players")

local dss = game:GetService("DataStoreService")
local bans = dss:GetDataStore("BanData")

event.OnServerEvent:Connect(function(playerWhoSent, text, reason)
	local UserID = players:FindFirstChild(text).UserId
	print(UserID)
	local success, errormessage = pcall(function()
		bans:SetAsync(UserID, true)
	end)
	
	if success then
		print(text, "is now banned!")
	end
	
	players:FindFirstChild(text):Kick("You have been banned from this Flybe experience due to: "..reason)
end)

game.Players.PlayerAdded:Connect(function(p)
	local id = p.UserId
	
	if bans:GetAsync(id) then
		p:Kick("You are banned from this Flybe experience.")
	end
	return false
end)

The RemoteEvent fires and the player is kicked but they are not kicked when they rejoin.

Any help is appreciated, thanks!

This might be because PlayerAdded is not getting called (This happens when the server is created, and the player joins at the same time)
This could fix it:

local Players = game:GetService("Players")

local function playerAdded(player: Player)
	-- code
end

Players.PlayerAdded:Connect(playerAdded)

for _, player in Players:GetPlayers() do
	task.spawn(playerAdded, player)
end

That worked perfectly, thank you so much!

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