[Solved] Trying to get all instances to kick players with their names not working

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    I want to create a script that gets the objects in a folder (Player Names) and compares them to all the players in the game, and if it’s a match kick the player.
  2. What is the issue? Include screenshots / videos if possible!
    It seems that it does nothing.
  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I tried to change around some stuff, but it fails to work still

My code:

function PlayerAdded()
	for _, Player in pairs(script.Parent.Players:GetChildren()) do
		if Player then
			for _, Player2 in pairs(game.Players:GetPlayers()) do
				if Player.Name == Player2.Name then
					Player2:Kick('You have been banned from this server via script "'.. Player:FindFirstChildWhichIsA("SoundGroup").Name '"')
				end
			end
		end
	end
	for _, Player in pairs(script.Parent.ManualBanList:GetChildren()) do
		if Player then
			for _, Player2 in pairs(game.Players:GetPlayers()) do
				if Player.Name == Player2.Name then
					Player2:Kick("You have been manually banned from the game by ".. Player:FindFirstChildWhichIsA("SoundGroup").Name)
				end
			end
		end
	end
end
game.Players.PlayerAdded:Connect(PlayerAdded)

(I know going through all players is a waste of time and code and I will look into a way to fix that later)
Screenshot_20221125_095140
Everything in the Parented folder of the script (ServerBanList)
(Everything else works)

1 Like

I fixed the issue, I forgot to put “…” after .Name

Fixed Code:

function PlayerAdded(Plr)
	for _, Player in pairs(script.Parent.Players:GetChildren()) do
		if Player then
				if Player.Name == Plr.Name then
					Plr:Kick('You have been banned from this server via script "'.. Player:FindFirstChildWhichIsA("SoundGroup").Name ..'"')
				end
			end
		end
	for _, Player in pairs(script.Parent.ManualBanList:GetChildren()) do
		if Player then
		if Player.Name == Plr.Name then
			Plr:Kick("You have been manually banned from the game by ".. Player:FindFirstChildWhichIsA("SoundGroup").Name)
				end
			end
		end
end
game.Players.PlayerAdded:Connect(PlayerAdded)

I’m so dumb I didn’t notice that the .Name was green

If I understand things correctly, you want to kick any player in the ban-list as soon as they join. If that is the case, I’d use user-id instead of name, as the name can change but user-id can’t.

Storing banlist:

You want to have an efficient container for the names, if you think it will be a pretty small list you can just use a regular list for it, otherwise I’d recommend looking into binary-search trees (BSTs), Tries or bucketing. A good practice is to let the container implement the search algorithm, therefore it has the function .Contains().

-- Module script containing the bans
local BanList = { ... } -- Should contain the user-ids of all banned users.

function BanList.Contains(userId: number): boolean -- Return true if userid is banned, else false.
    ... -- Relevant search algorithm
end

return BanList

Kicking banned players:

Next you want to kick all the players, this is pretty straight forward and is done in a similar fashion to how you already have done it.

local banlist = require(banlist) -- Get the banlist & its functions

game.Players.PlayerAdded:Connect(function(player: Player) -- Detect when player joins
    if banlist.Contains(player.UserId) then -- Check if user is on the ban-list
        player:Kick("Message") -- Kick if they are
    end
end)
2 Likes

Thank you for the help, but I already found the issue :grinning: :grinning: :grinning:
Edit: My way of doing it is really inefficient, if you want a good ban script use the Solution @ifkpop provided.

No problem, but I wouldn’t advise you to use the names to check if someone should be banned or not! It will allow users to by-pass the issue by changing their username, and it will ban anyone who has the name of someone who has been banned, even if they haven’t done anything wrong.

Ex:
Timothy_123 cheats, and gets banned. The username Timothy_123 is now added to the banlist. Timothy_123 notices that they get kicked when they join, therefore they try changing their username to Tim_321. They are now no longer banned. Timothy_521 wants to see if they can get a cooler name, so they change their name to Timothy_123. They are now banned from joining you game because of something the other Timothy_123 did.

Although this isn’t probably or likely to happen, at the very least not the accidental ban, it may still happen and should be something you account for. Once someone finds out you can evade the ban by changing username your ban system is more like a soft-ban than an actual ban.

1 Like

I know it’s very inefficient, but it’s local to the server. when the server closes the data is lost.
I like my idea because my tiny brain can easily make it temporary using game:GetService("Debris")
Edit: I decided to make the switch from Player.Name to Player.UserId
Edit2: The issue that ifkpop was bringing up was most likely the ManualBanList where the rules would apply

It stopped working after I changed Plr.Name to Plr.UserId

function PlayerAdded(Plr)
	for _, Player in pairs(script.Parent.Players:GetChildren()) do
		if Player then
			if Player.Name == Plr.UserId then
					Plr:Kick('You have been banned from this server via script "'.. Player:FindFirstChildWhichIsA("SoundGroup").Name ..'"')
				end
			end
		end
	for _, Player in pairs(script.Parent.ManualBanList:GetChildren()) do
		if Player then
			if Player.Name == Plr.UserId then
			Plr:Kick("You have been manually banned from the game by ".. Player:FindFirstChildWhichIsA("SoundGroup").Name)
				end
			end
		end
end
game.Players.PlayerAdded:Connect(PlayerAdded)

The Instance with the UserId is created but doesn’t get checked properly
Edit: After changing the Atmospheres to IntValues it worked correctly after using the IntValue’s .Value property.

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