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




