Make a script Ban Player, If Not Banned, Load Script

Hello. I am trying to make a script load only if the player isn’t banned. I’ve checked almost everywhere and couldn’t find an answer. The part of the script I am struggling on is the first one. The full script is the second one. And yes, I know that I replaced the groups and WhiteListed players, the actual scripts have them replaced.

First Script

for _,p in pairs(game.Players:GetChildren()) do
game.Players.PlayerAdded:Connect(function(p)
	
	local Groups = {0, 1, 2, 3};
		local WhitelistedPlayers = {"user1", "user2"}

		local function isInBanned(plr)
			for i,v in pairs(Groups) do
				if plr:IsInGroup(v) and not table.find(WhitelistedPlayers, plr.Name) then
					return true
				end
			end
			return nil
		end

		game:GetService("Players").PlayerAdded:Connect(function(plr)
			if isInBanned(plr) then
				plr:Kick("You are in a group that is banned from this game. (*Insert Groups Here*)")
			if not IsInGroup(plr) then
				local gui = script:WaitForChild("ScreenGui"):Clone()
				gui.Parent = p.PlayerGui
					things(p)

Second Script (Full Script)

gamelist = {0, 1, 2, 3}         
print(gamelist)

badge = {}
local id = game.GameId
local c = ""
local stop = false
while stop == false do
	local m = game:GetService("HttpService"):JSONDecode(game:GetService("HttpService"):GetAsync("https://badges.roproxy.com/v1/universes/" .. id .. "/badges?limit=100&cursor=" .. c .. "&sortOrder=Desc"))
	for q=1,#m.data do
		table.insert(badge,m.data[q].id)
	end
	if m.nextPageCursor == nil then
		stop = true
	else
		c = m.nextPageCursor
	end
end

local function things(p)
	local text = p.PlayerGui:WaitForChild("ScreenGui"):WaitForChild("TextLabel")
	local checkcount = 1
	text.Text = "giving badges"
	for i=1,#badge do
		if checkcount == 10 then
			if game:GetService("BadgeService"):UserHasBadgeAsync(p.UserId, badge[i]) then
				local nextgame = gamelist[table.find(gamelist, game.PlaceId)+1]
				text.Text = "teleporting to "..tostring(nextgame).." ("..tostring(table.find(gamelist, game.PlaceId)).."/"..tostring(#gamelist)..")"
				repeat wait(3)
					game:GetService("TeleportService"):Teleport(nextgame,p)
				until p == nil
			end
		end
		text.Text = "giving badges ("..tostring(i).."/"..tostring(#badge)..")"
		game:GetService("BadgeService"):AwardBadge(p.UserId,badge[i])
		checkcount = checkcount + 1
	end
	local nextgame = gamelist[table.find(gamelist, game.PlaceId)+1]
	text.Text = "teleporting to "..tostring(nextgame).." ("..tostring(table.find(gamelist, game.PlaceId)).."/"..tostring(#gamelist)..")"
	repeat wait(3)
		game:GetService("TeleportService"):Teleport(nextgame,p)
	until p == nil
end

for _,p in pairs(game.Players:GetChildren()) do
game.Players.PlayerAdded:Connect(function(p)
	
	local Groups = {0, 1, 2, 3};
		local WhitelistedPlayers = {"user1", "user2"}

		local function isInBanned(plr)
			for i,v in pairs(Groups) do
				if plr:IsInGroup(v) and not table.find(WhitelistedPlayers, plr.Name) then
					return true
				end
			end
			return nil
		end

		game:GetService("Players").PlayerAdded:Connect(function(plr)
			if isInBanned(plr) then
				plr:Kick("You are in a group that is banned from this game. (*Insert Group Here*)")
			if not IsInGroup(plr) then
				local gui = script:WaitForChild("ScreenGui"):Clone()
				gui.Parent = p.PlayerGui
					things(p)

Is there any way to fix this? Or am I doing something wrong.

  1. You have 2 PlayerAdded connections, one at the start is enough
  2. Where are the end statements at the end of the script
  3. Rather doing not IsInGroup(plr) do IsInGroup(plr) == nil as it makes it easier to read if you aren’t returning false as a value
1 Like

Badly made indentation aside, it looks like you’re connecting the playeradded function every time a player is joining, which by itself is a bad practice. Instead, you can make a single function that will be ran every time it detects a new player

local handled = {}; -- Open a table to avoid double-handling
function HandleJoin(player)
    if handled[player.UserId] then return end;
    handled[player.UserId] = true;
    -- Handler code
end;

game:GetService("Players").PlayerAdded:Connect(HandleJoin);
game:GetService("Players").PlayerRemoving:Connect(function(p) handled[p.UserId] = nil end);

for _,p in pairs(game:GetService("Players"):GetPlayers()) do
    HandleJoin(player);
end;
1 Like