local function g(g)g.CharacterAdded:Connect(function(n)local D=n:WaitForChild("\072\117\109\097\110\111\105\100")local t=main.pd[g]if t then for n,D in pairs(t.CommandsActive)do local t=g local C={g}local w=main.commands[string.lower(n)];(main:GetModule("\099\102")):ExecuteCommand(t,C,w,{ExtraDetails={DontForceActivate=true}})end for n,D in pairs(t.Items)do(main:GetModule("\069\120\116\101\110\115\105\111\110\115")):SetupItem(g,n,true)end end wait(1)if settingUpPlayer[g]then repeat wait(.1)until not settingUpPlayer[g]end end)local n=g.UserId if main.banned[tostring(n)]then g: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[g]=true local D=(main:GetModule("\080\108\097\121\101\114\068\097\116\097")):SetupPlayerData(g)settingUpPlayer[g]=nil if not main.players:GetPlayerByUserId(n)then return end g.Chatted:Connect(function(n)main.signals.PlayerChatted:Fire(g,n);(main:GetModule("\080\097\114\115\101\114")):ParseMessage(g,n,true)end)local t=(main:GetModule("\099\102")):GetFriends(n)if not main.pd[g]then return end main.pd[g].Friends=t if not main.players:GetPlayerByUserId(n)then return end for n,D in pairs(D.DefaultSettings)do local t=main.settings[n]if t~=D then main.pd[g].DefaultSettings[n]=t;(main:GetModule("\080\108\097\121\101\114\068\097\116\097")):ChangeStat(g,n,t)end end;(main:GetModule("\080\108\097\121\101\114\068\097\116\097")):ChangeStat(g,"\083\101\116\117\112\068\097\116\097",true)if n==main.ownerId or(main:GetModule("\099\102")):FindValue(main.owner,n)then(main:GetModule("\099\102")):RankPlayerSimple(g,5,true)if main.oldLoader then main.signals.ShowWarning:FireClient(g,"\079\108\100\076\111\097\100\101\114")end end local C=main.serverAdmins[g.Name]if C then(main:GetModule("\099\102")):RankPlayerSimple(g,C)end local w=main.permissions.specificUsers[g.Name]if w then(main:GetModule("\099\102")):RankPlayerSimple(g,w,true)end for t,C in pairs(main.permissions.gamepasses)do local w=false if(main:GetModule("\099\102")):FindValue(D.Gamepasses,t)then w=true elseif main.marketplaceService:UserOwnsGamePassAsync(n,t)then w=true;(main:GetModule("\080\108\097\121\101\114\068\097\116\097")):InsertStat(g,"\071\097\109\101\112\097\115\115\101\115",t);(main:GetModule("\099\102")):CheckAndRankToDonor(g,D,t)end if w then(main:GetModule("\099\102")):RankPlayerSimple(g,C.Rank,true)end end if not main.players:GetPlayerByUserId(n)then return end for n,t in pairs(main.permissions.assets)do local C=false if(main:GetModule("\099\102")):FindValue(D.Assets,n)then C=true elseif main.marketplaceService:PlayerOwnsAsset(g,n)then C=true;(main:GetModule("\080\108\097\121\101\114\068\097\116\097")):InsertStat(g,"\065\115\115\101\116\115",n)end if C then(main:GetModule("\099\102")):RankPlayerSimple(g,t.Rank,true)end end if not main.players:GetPlayerByUserId(n)then return end for n,D in pairs(main.permissions.groups)do local t=g:GetRoleInGroup(n)if D.Roles[t]then(main:GetModule("\099\102")):RankPlayerSimple(g,D.Roles[t].Rank,true)end end if not main.players:GetPlayerByUserId(n)then return end for n,D in pairs(t)do if D==main.ownerId then(main:GetModule("\099\102")):RankPlayerSimple(g,main.permissions.friends,true)end end local o=game.VIPServerOwnerId if o~=0 then if n==o then(main:GetModule("\099\102")):RankPlayerSimple(g,main.permissions.vipServerOwner,true)else(main:GetModule("\099\102")):RankPlayerSimple(g,main.permissions.vipServerPlayer,true)end end local F=main.permissions.freeAdmin if tonumber(F)and F>0 then(main:GetModule("\099\102")):RankPlayerSimple(g,F,true)end if D.Rank<main.ranksAllowedToJoin then g: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 n,t in pairs(main.players:GetChildren())do main.signals.Hint:FireClient(t,{"\083\116\097\110\100\097\114\100";"\083\101\114\118\101\114 \076\111\099\107\058 "..(g.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(D.Rank).."\'"))),Color3.fromRGB(255,255,255)})end end local y,c=pcall(function()return main.serverBans[(main:GetModule("\099\102")):FindUserIdInRecord(main.serverBans,n)]end)if not c then y,c=pcall(function()return main.sd.Banland.Records[(main:GetModule("\099\102")):FindUserIdInRecord(main.sd.Banland.Records,n)]end)end if y and c then(main:GetModule("\099\102")):BanPlayer(g.UserId,c)end if D.SaveRank and(main:GetModule("\099\102")):GetRankName(D.Rank)==""then(main:GetModule("\099\102")):Unrank(g)end wait(2)if not main.players:GetPlayerByUserId(n)then return end local X for g,n in pairs(main.settings.ChatColors)do if D.Rank==g then X=n break end end if X then local D=main.chatService:GetSpeaker(g.Name)if not D then local t=10 for t=1,t,1 do wait(.5)if not main.players:GetPlayerByUserId(n)then return end D=main.chatService:GetSpeaker(g.Name)if D then break end end end if D then D:SetExtraData("\067\104\097\116\067\111\108\111\114",X)end end wait(1)if not main.players:GetPlayerByUserId(n)then return end if D.Rank>0 and main.settings.WelcomeRankNotice~=false then local n=(main:GetModule("\099\102")):GetRankName(D.Rank);(main:GetModule("\099\102")):FormatAndFireNotice(g,"\087\101\108\099\111\109\101\082\097\110\107",n)end if D.Donor and main.settings.WelcomeDonorNotice~=false then(main:GetModule("\099\102")):DonorNotice(g,D)end end

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




