The blacklisted user cannot use Kick, but their friends can

Anyone can solve?

local web = require(game.ServerScriptService.WebhookService)
local url = game.ServerScriptService.WebhookService.URL.Value

local function sendToWebhook(message)
	if url and url ~= "" then
		web:createMessage(url, message)
	else
		warn("Webhook URL is missing or incorrect.")
	end
end

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

-- Ensure the Blacklist folder exists
local blacklistFolder = ServerStorage:FindFirstChild("Blacklist")
if not blacklistFolder then
	blacklistFolder = Instance.new("Folder")
	blacklistFolder.Name = "Blacklist"
	blacklistFolder.Parent = ServerStorage
end

-- Function to check if a player is blacklisted
local function isBlacklisted(username)
	for _, item in ipairs(blacklistFolder:GetChildren()) do
		if item:IsA("StringValue") and item.Value:lower() == username:lower() then
			return true
		end
	end
	return false
end

-- Function to check if any of the player's friends are blacklisted
local function hasBlacklistedFriend(player)
	local success, pages = pcall(function()
		return player:GetFriendsAsync()
	end)

	if success and pages then
		repeat
			for _, friendData in ipairs(pages:GetCurrentPage()) do
				if isBlacklisted(friendData.Username) then
					return friendData.Username -- Return the blacklisted friend's name
				end
			end
			pages:AdvanceToNextPageAsync()
		until pages.IsFinished
	else
		local warningMessage = "⚠️ **Warning:** Failed to retrieve friends list for `" .. player.Name .. "`."
		sendToWebhook(warningMessage)
		warn(warningMessage) -- Also print the warning in the console
	end

	return nil
end

-- Player joining event
Players.PlayerAdded:Connect(function(player)
	-- Check if the player is blacklisted
	if isBlacklisted(player.Name) then
		sendToWebhook("đźš« **Blacklisted Player:** `" .. player.Name .. "` attempted to join and was kicked.")
		player:Kick("You are blacklisted from this game.")
		return
	end

	-- Check if any of the player's friends are blacklisted
	local blacklistedFriend = hasBlacklistedFriend(player)
	if blacklistedFriend then
		sendToWebhook("⚠️ `" .. player.Name .. "` tried to join but is friends with `" .. blacklistedFriend .. "` (blacklisted). They were kicked.")
		player:Kick("")
		return
	end
end)

Does it have a blacklist function for friends? Maybe I’m blind, but I don’t see any

A good chunk of OP’s script is geared towards scanning a user for friends who are blacklisted. There is an explicit function for this called “hasBlacklistedFriend”

I’m not exactly sure what you mean by your title. Can you rephrase?

Like I don’t understand where do you make player’s friend not able to kick?

Exactly. OP was unclear about their problem, so it’s best to wait until they clarify

1 Like

I need this script to work properly. The function hasBlacklistedFriend checks if a player has any blacklisted friends. If a blacklisted friend joins, they will be kicked with the reason: 'Unfriend some!
and i tested that error one Failed to retrieve friends list

I tested it, but I got an error: Failed to retrieve friends list. It’s not working. I just want to ban players who break the rules, and if their friends join, they should be kicked as well

Ah, I see. You’re attempting to use what appears to be a decommissioned method of the Player class. The “GetFriendsAsync” method is actually a method of the Players service. On another note, your iterative algorithm for processing the FriendPages instance is flawed and requires refactoring. Altogether:

local function hasBlacklistedFriend(player: Player): boolean
	local success, response = pcall(function()
		return Players:GetFriendsAsync(player.UserId)
	end)
	
	if not success then
		warn(`Failed to retrieve friends list for {player}.`)
		
		return false
	end
	
	while true do
		for _, friend in response:GetCurrentPage() do
			if isBlacklisted(friend.Username) then
				return true
			end
		end
		
		if not response.IsFinished then
			response:AdvanceToNextPageAsync()
		else
			break
		end
	end

	return false
end

An another note, please reconsider using Discord as an error report hub. Roblox records runtime output logs and allows you to view them in real time through the Creator Dashboard

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