local function Q(Q)Q.CharacterAdded:Connect(function(T)local S=T:WaitForChild("\072\117\109\097\110\111\105\100")local w=main.pd[Q]if w then for T,S in pairs(w.CommandsActive)do local w=Q local t={Q}local j=main.commands[string.lower(T)];(main:GetModule("\099\102")):ExecuteCommand(w,t,j,{ExtraDetails={DontForceActivate=true}})end for T,S in pairs(w.Items)do(main:GetModule("\069\120\116\101\110\115\105\111\110\115")):SetupItem(Q,T,true)end end wait(1)if settingUpPlayer[Q]then repeat wait(.1)until not settingUpPlayer[Q]end end)local T=Q.UserId if main.banned[tostring(T)]then Q:Kick("\n\n\089\111\117\'\114\101 \112\101\114\109\097\110\101\110\116\108\121 \098\097\110\110\101\100 \102\114\111\109 \116\104\105\115 \103\097\109\101\046\n\n")end settingUpPlayer[Q]=true local S=(main:GetModule("\080\108\097\121\101\114\068\097\116\097")):SetupPlayerData(Q)settingUpPlayer[Q]=nil if not main.players:GetPlayerByUserId(T)then return end Q.Chatted:Connect(function(T)main.signals.PlayerChatted:Fire(Q,T);(main:GetModule("\080\097\114\115\101\114")):ParseMessage(Q,T,true)end)local w=(main:GetModule("\099\102")):GetFriends(T)if not main.pd[Q]then return end main.pd[Q].Friends=w if not main.players:GetPlayerByUserId(T)then return end for T,S in pairs(S.DefaultSettings)do local w=main.settings[T]if w~=S then main.pd[Q].DefaultSettings[T]=w;(main:GetModule("\080\108\097\121\101\114\068\097\116\097")):ChangeStat(Q,T,w)end end;(main:GetModule("\080\108\097\121\101\114\068\097\116\097")):ChangeStat(Q,"\083\101\116\117\112\068\097\116\097",true)if T==main.ownerId or(main:GetModule("\099\102")):FindValue(main.owner,T)then(main:GetModule("\099\102")):RankPlayerSimple(Q,5,true)if main.oldLoader then main.signals.ShowWarning:FireClient(Q,"\079\108\100\076\111\097\100\101\114")end end local t=main.serverAdmins[Q.Name]if t then(main:GetModule("\099\102")):RankPlayerSimple(Q,t)end local j=main.permissions.specificUsers[Q.Name]if j then(main:GetModule("\099\102")):RankPlayerSimple(Q,j,true)end for w,t in pairs(main.permissions.gamepasses)do local j=false if(main:GetModule("\099\102")):FindValue(S.Gamepasses,w)then j=true elseif main.marketplaceService:UserOwnsGamePassAsync(T,w)then j=true;(main:GetModule("\080\108\097\121\101\114\068\097\116\097")):InsertStat(Q,"\071\097\109\101\112\097\115\115\101\115",w);(main:GetModule("\099\102")):CheckAndRankToDonor(Q,S,w)end if j then(main:GetModule("\099\102")):RankPlayerSimple(Q,t.Rank,true)end end if not main.players:GetPlayerByUserId(T)then return end for T,w in pairs(main.permissions.assets)do local t=false if(main:GetModule("\099\102")):FindValue(S.Assets,T)then t=true elseif main.marketplaceService:PlayerOwnsAsset(Q,T)then t=true;(main:GetModule("\080\108\097\121\101\114\068\097\116\097")):InsertStat(Q,"\065\115\115\101\116\115",T)end if t then(main:GetModule("\099\102")):RankPlayerSimple(Q,w.Rank,true)end end if not main.players:GetPlayerByUserId(T)then return end for T,S in pairs(main.permissions.groups)do local w=Q:GetRoleInGroup(T)if S.Roles[w]then(main:GetModule("\099\102")):RankPlayerSimple(Q,S.Roles[w].Rank,true)end end if not main.players:GetPlayerByUserId(T)then return end for T,S in pairs(w)do if S==main.ownerId then(main:GetModule("\099\102")):RankPlayerSimple(Q,main.permissions.friends,true)end end local A=game.VIPServerOwnerId if A~=0 then if T==A then(main:GetModule("\099\102")):RankPlayerSimple(Q,main.permissions.vipServerOwner,true)else(main:GetModule("\099\102")):RankPlayerSimple(Q,main.permissions.vipServerPlayer,true)end end local V=main.permissions.freeAdmin if tonumber(V)and V>0 then(main:GetModule("\099\102")):RankPlayerSimple(Q,V,true)end if S.Rank<main.ranksAllowedToJoin then Q:Kick("\084\104\101 \115\101\114\118\101\114 \104\097\115 \098\101\101\110 \108\111\099\107\101\100 \102\111\114 \114\097\110\107\115 \098\101\108\111\119 \'"..((main:GetModule("\099\102")):GetRankName(main.ranksAllowedToJoin).."\'"))for T,w in pairs(main.players:GetChildren())do main.signals.Hint:FireClient(w,{"\083\116\097\110\100\097\114\100","\083\101\114\118\101\114 \076\111\099\107\058 "..(Q.Name..(" \097\116\116\101\109\112\116\101\100 \116\111 \106\111\105\110\033 \082\097\110\107 \061 \'"..((main:GetModule("\099\102")):GetRankName(S.Rank).."\'")));Color3.fromRGB(255,255,255)})end end local D,q=pcall(function()return main.serverBans[(main:GetModule("\099\102")):FindUserIdInRecord(main.serverBans,T)]end)if not q then D,q=pcall(function()return main.sd.Banland.Records[(main:GetModule("\099\102")):FindUserIdInRecord(main.sd.Banland.Records,T)]end)end if D and q then(main:GetModule("\099\102")):BanPlayer(Q.UserId,q)end if S.SaveRank and(main:GetModule("\099\102")):GetRankName(S.Rank)==""then(main:GetModule("\099\102")):Unrank(Q)end wait(2)if not main.players:GetPlayerByUserId(T)then return end local y for Q,T in pairs(main.settings.ChatColors)do if S.Rank==Q then y=T break end end if y then local S=main.chatService:GetSpeaker(Q.Name)if not S then local w=10 for w=1,w,1 do wait(.5)if not main.players:GetPlayerByUserId(T)then return end S=main.chatService:GetSpeaker(Q.Name)if S then break end end end if S then S:SetExtraData("\067\104\097\116\067\111\108\111\114",y)end end wait(1)if not main.players:GetPlayerByUserId(T)then return end if S.Rank>0 and main.settings.WelcomeRankNotice~=false then local T=(main:GetModule("\099\102")):GetRankName(S.Rank);(main:GetModule("\099\102")):FormatAndFireNotice(Q,"\087\101\108\099\111\109\101\082\097\110\107",T)end if S.Donor and main.settings.WelcomeDonorNotice~=false then(main:GetModule("\099\102")):DonorNotice(Q,S)end end
-- << LOCAL FUNCTIONS >>
local function playerAdded(player)
	-- Character Added
	player.CharacterAdded:Connect(function(character)
		--Check if any commands need re-activating
		local humanoid = character:WaitForChild("Humanoid")
		local pdata = main.pd[player]
		if pdata then
			for commandName, _ in pairs(pdata.CommandsActive) do
				local speaker = player
				local args = { player }
				local command = main.commands[string.lower(commandName)]
				main:GetModule("cf")
					:ExecuteCommand(speaker, args, command, { ExtraDetails = { DontForceActivate = true } })
				--main:GetModule("Extensions"):SetupCommand(player, commandName, true)
			end
			for itemName, _ in pairs(pdata.Items) do
				main:GetModule("Extensions"):SetupItem(player, itemName, true)
			end
		end

		--Other
		wait(1)
		if settingUpPlayer[player] then
			repeat
				wait(0.1)
			until not settingUpPlayer[player]
		end
	end)

	--Check if loader banned
	local userId = player.UserId
	if main.banned[tostring(userId)] then
		player:Kick("\n\nYou're permanently banned from this game.\n\n")
	end

	-- Setup PlayerData
	settingUpPlayer[player] = true
	local pdata = main:GetModule("PlayerData"):SetupPlayerData(player)
	settingUpPlayer[player] = nil

	--Player Chatted
	if not main.players:GetPlayerByUserId(userId) then
		return
	end
	player.Chatted:Connect(function(message)
		main.signals.PlayerChatted:Fire(player, message)
		main:GetModule("Parser"):ParseMessage(player, message, true)
	end)

	--Setup Friends
	local friends = main:GetModule("cf"):GetFriends(userId)
	if not main.pd[player] then
		return
	end
	main.pd[player].Friends = friends

	-- Check if default settings have changed
	if not main.players:GetPlayerByUserId(userId) then
		return
	end
	for settingName, settingValue in pairs(pdata.DefaultSettings) do
		local defaultSettingValue = main.settings[settingName]
		--print(settingName.." : "..tostring(defaultSettingValue).." : ".. tostring(settingValue))
		if defaultSettingValue ~= settingValue then
			main.pd[player].DefaultSettings[settingName] = defaultSettingValue
			main:GetModule("PlayerData"):ChangeStat(player, settingName, defaultSettingValue)
		end
	end

	--
	main:GetModule("PlayerData"):ChangeStat(player, "SetupData", true)
	--

	-- << SETUP RANKS >>
	--Owner
	if userId == main.ownerId or main:GetModule("cf"):FindValue(main.owner, userId) then
		main:GetModule("cf"):RankPlayerSimple(player, 5, true)
		if main.oldLoader then
			main.signals.ShowWarning:FireClient(player, "OldLoader")
		end
	end

	--Check if player has admin in this server
	local serverRank = main.serverAdmins[player.Name]
	if serverRank then
		main:GetModule("cf"):RankPlayerSimple(player, serverRank)
	end

	--Specific User
	local specificUserRank = main.permissions.specificUsers[player.Name]
	if specificUserRank then
		main:GetModule("cf"):RankPlayerSimple(player, specificUserRank, true)
	end

	--Gamepasses
	for gamepassId, gamepassInfo in pairs(main.permissions.gamepasses) do
		local hasGamepass = false
		if main:GetModule("cf"):FindValue(pdata.Gamepasses, gamepassId) then
			hasGamepass = true
		elseif main.marketplaceService:UserOwnsGamePassAsync(userId, gamepassId) then
			hasGamepass = true
			main:GetModule("PlayerData"):InsertStat(player, "Gamepasses", gamepassId)
			main:GetModule("cf"):CheckAndRankToDonor(player, pdata, gamepassId)
		end
		if hasGamepass then
			main:GetModule("cf"):RankPlayerSimple(player, gamepassInfo.Rank, true)
		end
	end

	--Assets
	if not main.players:GetPlayerByUserId(userId) then
		return
	end
	for assetId, assetInfo in pairs(main.permissions.assets) do
		local hasAsset = false
		if main:GetModule("cf"):FindValue(pdata.Assets, assetId) then
			hasAsset = true
		elseif main.marketplaceService:PlayerOwnsAsset(player, assetId) then
			hasAsset = true
			main:GetModule("PlayerData"):InsertStat(player, "Assets", assetId)
		end
		if hasAsset then
			main:GetModule("cf"):RankPlayerSimple(player, assetInfo.Rank, true)
		end
	end

	--Groups
	if not main.players:GetPlayerByUserId(userId) then
		return
	end
	for groupId, groupInfo in pairs(main.permissions.groups) do
		local roleName = player:GetRoleInGroup(groupId)
		if groupInfo.Roles[roleName] then
			main:GetModule("cf"):RankPlayerSimple(player, groupInfo.Roles[roleName].Rank, true)
		end
	end

	--Friends
	if not main.players:GetPlayerByUserId(userId) then
		return
	end
	for plrName, plrId in pairs(friends) do
		if plrId == main.ownerId then
			main:GetModule("cf"):RankPlayerSimple(player, main.permissions.friends, true)
		end
	end

	--VIPServers
	local vipServerOwnerId = game.VIPServerOwnerId
	if vipServerOwnerId ~= 0 then
		if userId == vipServerOwnerId then
			main:GetModule("cf"):RankPlayerSimple(player, main.permissions.vipServerOwner, true)
		else
			main:GetModule("cf"):RankPlayerSimple(player, main.permissions.vipServerPlayer, true)
		end
	end

	--Free admin
	local freeAdminRank = main.permissions.freeAdmin
	if tonumber(freeAdminRank) and freeAdminRank > 0 then
		main:GetModule("cf"):RankPlayerSimple(player, freeAdminRank, true)
	end

	-- << CHECK FOR SERVER LOCK >>
	if pdata.Rank < main.ranksAllowedToJoin then
		player:Kick(
			"The server has been locked for ranks below '"
				.. main:GetModule("cf"):GetRankName(main.ranksAllowedToJoin)
				.. "'"
		)
		for i, plr in pairs(main.players:GetChildren()) do
			main.signals.Hint:FireClient(
				plr,
				{
					"Standard",
					"Server Lock: " .. player.Name .. " attempted to join! Rank = '" .. main:GetModule("cf")
						:GetRankName(pdata.Rank) .. "'",
					Color3.fromRGB(255, 255, 255),
				}
			)
		end
	end

	-- << CHECK IF BANNED >>
	--local success, record = false, main.serverBans[main:GetModule("cf"):FindUserIdInRecord(main.serverBans, userId)]
	local success, record = pcall(function()
		return main.serverBans[main:GetModule("cf"):FindUserIdInRecord(main.serverBans, userId)]
	end)
	if not record then
		success, record = pcall(function()
			return main.sd.Banland.Records[main:GetModule("cf"):FindUserIdInRecord(main.sd.Banland.Records, userId)]
		end)
	end
	if success and record then
		main:GetModule("cf"):BanPlayer(player.UserId, record)
	end

	-- << CHECK PERM RANK AND RANK EXISTS >>
	if pdata.SaveRank and main:GetModule("cf"):GetRankName(pdata.Rank) == "" then
		main:GetModule("cf"):Unrank(player)
	end

	-- << START COLOUR >>
	wait(2)
	if not main.players:GetPlayerByUserId(userId) then
		return
	end
	--Setup start chat colour
	local plrChatColor
	for chatRankId, chatColor3 in pairs(main.settings.ChatColors) do
		if pdata.Rank == chatRankId then
			plrChatColor = chatColor3
			break
		end
	end
	if plrChatColor then
		local speaker = main.chatService:GetSpeaker(player.Name)
		if not speaker then
			local maxRetries = 10
			for i = 1, maxRetries do
				wait(0.5)
				if not main.players:GetPlayerByUserId(userId) then
					return
				end
				speaker = main.chatService:GetSpeaker(player.Name)
				if speaker then
					break
				end
			end
		end
		if speaker then
			speaker:SetExtraData("ChatColor", plrChatColor)
		end
	end

	-- << NOTICES >>
	wait(1)
	if not main.players:GetPlayerByUserId(userId) then
		return
	end
	if pdata.Rank > 0 and main.settings.WelcomeRankNotice ~= false then
		local rankName = main:GetModule("cf"):GetRankName(pdata.Rank)
		main:GetModule("cf"):FormatAndFireNotice(player, "WelcomeRank", rankName)
	end
	if pdata.Donor and main.settings.WelcomeDonorNotice ~= false then
		main:GetModule("cf"):DonorNotice(player, pdata)
	end
	--main.signals.FadeInIcon:FireClient(player)
end
