I have been trying to work with collisions (specifically with characters) and this script should be working according to Roblox wiki, and gives no errors, so I've come to ask you guys what you think the issue is. I've checked, and I know that the first part works to put the children of the parent in the collision group, so the issue is the second part where they add the player parts, but I still can't figure what is wrong. Anything helps.

local work = script.Parent 
local PhysicsService = game:GetService("PhysicsService")
local Players = game:GetService("Players")
local admin = "admin"
local allranks = {} -- table where all the ranks are stored

for i, v in pairs(work:GetChildren()) do -- goes through the parent containing all the objects that shouldn't collide with players (it's children should not collide)
	if v.Name == "Group Rank Door" then
		local config = v:FindFirstChild("Configuration")
		if config then
			local rankid = config:FindFirstChild("RankId")
			if rankid then
				local TheRankId = config:WaitForChild("RankId")
				local isrkn = false
				for i = 1, #allranks  do -- sees if such rank exists to decide if it should create a new rank
					if allranks[i] == TheRankId.Value then
						isrkn = true
					end
				end
				if isrkn == false then
					allranks[#allranks + 1] = TheRankId.Value
					print(TheRankId.Value)
					PhysicsService:CreateCollisionGroup(tostring(TheRankId.Value).. "Num") -- creates the collision group with the id followed by "num"
					PhysicsService:CreateCollisionGroup(tostring(TheRankId.Value).."Players") -- creates another one with players and id
					PhysicsService:SetPartCollisionGroup(v, tostring(TheRankId.Value) .. "Num") -- adds the specific object in list to the non-player collision group
					PhysicsService:CollisionGroupSetCollidable(tostring(TheRankId.Value).. "Num",tostring(TheRankId.Value).."Players", false) -- makes it so the two groups don't collide
				end
			end
		end
	end
	wait()
end
local function setCollisionGroupRecursive(object, groupName)
	if object:IsA("BasePart") then
		PhysicsService:SetPartCollisionGroup(object, groupName) -- would set the part in the collision group if it is basepart
	end
	for _, child in ipairs(object:GetChildren()) do -- otherwise, it would list through it's children
		setCollisionGroupRecursive(child, groupName)
	end
end

local function onCharacterAdded(character, rank)-- when character is added 
	for i = 1, #allranks do -- nil for some reason
		if allranks[i] <= rank then -- every time a rank in the table is less, it would add all of the player's parts to it
			print("setting another one")
			local collisionGroupName = tostring(allranks[i]) .. "Players" -- makes the key to put in the player parts
			setCollisionGroupRecursive(character, collisionGroupName, allranks[i] .. "Num")
		end
	end
	
		
end
 
local function onPlayerAdded(player)-- when the player is added
	print("Ok done")
	player.CharacterAdded:Connect(function(character)
		
		local rank = player:GetRankInGroup(4921500) -- gets the rank of player (in this group) and then tells it to wait for character
		onCharacterAdded(character, rank)
	end)

end
Players.PlayerAdded:Connect(onPlayerAdded)


If you have any questions about some variable or aspect, just write a comment, but once again, anything helps. Thanks!