Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Is there an easier way to check if a player is in an array?

So I am trying to make a ban script. WHen a player bans somebody it inserts them into a table and if they rejoin then it kicks them from the server. I have it to ban one player but when I try to ban another player in the server they can rejoin. How can I make the script so if the person is in the array, they cannot rejoin?

`` local Webhook = "WEBHOOK HERE NOT PUTTING IT"

local Banned = {}

game.ReplicatedStorage.BanEvent.OnServerEvent:Connect(function(PlayerName,Player,Reason,Person) local thing = { content = Person.." Banned ".. Player .. " for ".. Reason } local DataCome = HTTP:JSONEncode(thing) HTTP:PostAsync(Webhook, DataCome)

local players = game:GetService("Players")

table.insert(Banned, Player)
game.Players:FindFirstChild(Player):Kick("You have been banned for ".. Reason)	


while true do wait()
	game.Players.PlayerAdded:Connect(function(plr)
		if plr.Name == Banned[1]  then
			plr:Kick('Backlisted from this server')
		end
	end)
	
end

end) ``

3 comments

0

If you want to make a Server-specific blacklist, a Dictionary cache is the best approach. Map each user's UserId to a truthy attribute and then simply index them–for a O(1) lookup complexy–under their UserId upon reconnection.

0

Note: You're perpetually spawning RBXScriptConnections to the same RBXScriptSignal with an identical Callback, this is unnecessary and is** causing a memory leak.

0

Keep the 'PlayerAdded' signal outside of the 'OnServerEvent' Callback and rid of your redundant loop. Then you can simply search for an entry under the UID you assigned.

1 answer

The main problem with your ban script is it won't really fire because your functions are right within "BanEvent."

local players = game:GetService("Players")
local Banned = {}

-- I made a mistake earlier
game.ReplicatedStorage.BanEvent.OnServerEvent:Connect(function(PlayerName, Reason, PersonWhoGotBanned)
-- "PlayerName" holds the name of the banned player simply by "PlayerName.Name"
-- Im going to assume this is correct and working.
local thing = { content = PlayerName.Name.." Banned ".. PersonWhoGotBanned .. " for ".. Reason }
local DataCome = HTTP:JSONEncode(thing)
HTTP:PostAsync(Webhook, DataCome)


table.insert(Banned, PersonWhoGotBanned)
if players[PersonWhoGotBanned] then
	players[PersonWhoGotBanned]:Kick("You have been banned for ".. Reason) 
else
	warn('Could not find', PersonWhoGotBanned)
end
end) -- End of BanEvent. Hopefully your remote event should still work. Otherwise ask again.

-- Leave this out of the "BanEvent." It will fire automatically once they rejoin.
players.PlayerAdded:Connect(function(player)
	for _, bannedPlayer in pairs(Banned) do -- Loop through the entire array
		if player.Name == bannedPlayer then -- Compare the items from that array with the player name
			player:Kick("You're banned from this server")
		else
			continue
		end
	end
end)

This discussion is preserved as it was. New answers, comments, and votes are closed.