Sometimes can't move when I first join into the game

Hi, so I have this script that makes it so when you first join into the game you will spawn as a random character but sometimes when you join into the game you can’t move and you need to rejoin, im pretty sure the reason why is because when you join into the game the humanoid root part hasnt loaded yet and it already changes you into a custom character, but i’m not sure and don’t know how to fix.

Script:

local Players = game:GetService("Players")
local PhysicsService = game:GetService("PhysicsService")
local SoundService = game:GetService("SoundService")
local charactersFolder = game.ServerStorage:WaitForChild("StarterCharacters")
local characters = charactersFolder:GetChildren()

-- Collision group setup
local GroupName = "Players"
PhysicsService:RegisterCollisionGroup(GroupName)
PhysicsService:CollisionGroupSetCollidable(GroupName, GroupName, false)

-- Function to assign collision groups to character parts
local function assignCollisionGroup(character)
	for _, part in ipairs(character:GetDescendants()) do
		if part:IsA("BasePart") then
			part.CollisionGroup = GroupName
		end
	end
end

-- Function to spawn the player at their saved stage
local function spawnPlayerAtStage(player, character)
	local stage = player:WaitForChild("leaderstats"):WaitForChild("Stage").Value

	-- Find the checkpoint corresponding to the stage
	local checkpoint = workspace.Checkpoints:FindFirstChild(tostring(stage))
	if checkpoint then
		local spawnCFrame = checkpoint.CFrame + Vector3.new(0, 3.25, 0)
		character:SetPrimaryPartCFrame(spawnCFrame)
	else
		warn("Checkpoint for stage " .. stage .. " not found!")
	end
end

-- Function to randomize the character
local function randomizeCharacter(player)
	if #characters > 0 then
		local starterChars = {}
		for _, char in ipairs(characters) do
			table.insert(starterChars, char)
		end

		-- Randomly select a character
		local random = Random.new()
		local selectedCharacter = starterChars[random:NextInteger(1, #starterChars)]:Clone()

		-- Replace the player's character
		local oldCharacter = player.Character
		if oldCharacter then
			local oldCFrame = oldCharacter:GetPrimaryPartCFrame()
			selectedCharacter:SetPrimaryPartCFrame(oldCFrame)
			oldCharacter:Destroy()
		end

		player.Character = selectedCharacter
		selectedCharacter.Parent = workspace

		-- Assign collision group
		assignCollisionGroup(selectedCharacter)

		return selectedCharacter
	else
		warn("No starter characters available in StarterCharacters folder.")
		return player.Character
	end
end

-- Function to reconnect tool sound after respawn with cooldown
local function reconnectToolSound(player)
	task.wait(1) -- Wait for the tool to load in Backpack
	local tool = player.Backpack:FindFirstChild("Speaker") -- Replace with your actual tool's name

	if tool then
		local sound = SoundService:FindFirstChild("QuackSound") -- Ensure the sound exists
		local reconnectSound = SoundService:FindFirstChild("ReconnectSound") -- New reconnect sound
		if sound then
			tool.Activated:Connect(function()
				-- Cooldown check to prevent immediate use
				if not tool:GetAttribute("Cooldown") then
					tool:SetAttribute("Cooldown", true)  -- Set cooldown attribute to true
					sound:Play()  -- Play the original tool sound

					-- Reset cooldown after 1.8 seconds (cooldown duration)
					task.wait(10)
					tool:SetAttribute("Cooldown", false)  -- Reset cooldown
				end
			end)
		else
			warn("QuackSound not found in SoundService!")
		end
		if reconnectSound then
			tool.Activated:Connect(function()
				-- Cooldown check to prevent immediate use
				if not tool:GetAttribute("Cooldown") then
					tool:SetAttribute("Cooldown", true)  -- Set cooldown attribute to true
					reconnectSound:Play()  -- Play the reconnect sound

					-- Reset cooldown after 1.8 seconds (cooldown duration)
					task.wait(10)
					tool:SetAttribute("Cooldown", false)  -- Reset cooldown
				end
			end)
		else
			warn("ReconnectSound not found in SoundService!")
		end
	end
end

-- Player Added logic
Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		-- Randomize the character
		local randomizedCharacter = randomizeCharacter(player)

		-- Spawn the player at their saved stage
		spawnPlayerAtStage(player, randomizedCharacter)

		-- Double-check the spawn position after a delay to ensure correctness
		task.delay(1, function()
			spawnPlayerAtStage(player, randomizedCharacter)
		end)

		-- Reconnect tool functionality after respawn
		reconnectToolSound(player)
	end)
end)

-- Save player's progress when they reach a checkpoint
workspace.Checkpoints.ChildAdded:Connect(function(checkpoint)
	if checkpoint:IsA("Part") then
		checkpoint.Touched:Connect(function(hit)
			local character = hit.Parent
			local player = Players:GetPlayerFromCharacter(character)
			if player then
				local stage = tonumber(checkpoint.Name)
				if stage then
					player.leaderstats.Stage.Value = stage
					print("Player " .. player.Name .. " reached stage " .. stage)
				end
			end
		end)
	end
end)

-- Reset player's stage to 0
script.Parent.MouseButton1Click:Connect(function()
	local player = script:FindFirstAncestorOfClass("Player")
	if player then
		player.leaderstats.Stage.Value = 0 -- Reset leaderstats.Stage
		print("Player " .. player.Name .. " reset to stage 0.")

		local character = player.Character
		local checkpoint = workspace.Checkpoints:FindFirstChild("0")
		if checkpoint and character then
			character:SetPrimaryPartCFrame(checkpoint.CFrame + Vector3.new(0, 3, 0))
		else
			warn("Checkpoint for stage 0 not found!")
		end
	end
end)