Sometimes cant move when I join into the game

Hi, I have a problem when sometimes join into the game I cant move, but it rarely happens and the only way to move is to rejoin the game, I think the problems is something with the humanoidrootpart, but im not sure how to fix.

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()

		-- Wait for the original character to fully load
		local oldCharacter = player.Character
		if not oldCharacter or not oldCharacter:FindFirstChild("HumanoidRootPart") then
			player.CharacterAdded:Wait()
			repeat
				task.wait(0.2) -- Keep checking until the character loads
			until player.Character and player.Character:FindFirstChild("HumanoidRootPart")
			oldCharacter = player.Character
		end

		-- Get the old character's position
		local oldCFrame = oldCharacter:GetPrimaryPartCFrame()
		selectedCharacter:SetPrimaryPartCFrame(oldCFrame)
		oldCharacter:Destroy()

		-- Assign the new character
		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 10 seconds
					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 10 seconds
					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)

3 Likes

There may be a problem with your code, but in Roblox, sometimes when you log into the game, it doesn’t respond when you press keys. Did the other keys work well when the character wasn’t moving?

1 Like

I couldnt move/jump but I could interact with the ui

1 Like

Did you press ‘ESC’ key and did Roblox menu open?

1 Like

yes, the roblox menu did open when I pressed the ESC key

1 Like

If so, that’s not the problem with Roblox. I’ll check your script.

1 Like

Where did you test it? On Roblox, or Roblox Studio?

1 Like

I test it on roblox and roblox studio, and the problem happens on both, but I think mostly roblox

1 Like

When you tested it on Roblox Studio, did you anchor and unanchor your character and it move?

1 Like

no sorry I never thought about that

1 Like

Or, how was HumanoidRootPart? Did it anchored?

1 Like

like was the humanoidrootpart of the custom character anchored? like in the game before tested like is that what you mean? I’m very sorry if I misunderstood your question

1 Like

I meant that.
⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀

1 Like

1 Like

Even now, when testing, does the character get stuck? If so, can you provide image or video that shows how the character anchored?

1 Like

well, it randomly gets stuck whenever I test it (Like whenever I first join in), but most of the time I can move properly so there is usually no problem. Very sorry but I gtg

1 Like

Sorry I had to go but I got a video of the problem now, in the video I tried to move/jump and it no work. oh ya please ignore the white frame I was testing something

1 Like

I checked your video. I saw “PlayerModule” was in the character. Why it was in the character?

1 Like

the player module was in the starterplayerscripts but since u spawn into the as a different character I didnt know If I needed to put that script in there so I did it just incase.
oh yea and could this be the problem of why sometimes when I join into the game I cant move?
18:06:40.553 Infinite yield possible on ’ :WaitForChild(“Humanoid”)’

Edit: I got the glitch again and I noticed that the ui always loads in first (Like super super fast) whenever I cant move

1 Like