Character spawns at incorrect spawn location

Hi! I’m having issues moving the player’s character when they spawn. I utilize CharacterAdded however, this is still unsufficient. The issue is hard to replicate as most of the time the character is moved as intended, but their humanoid root part isn’t always moved to the specified CFrame location. I suspect this is a result of the character having not finished loading so the logic to move their root has already run by the time their character loads and thus they spawn at a spawn location in the workspace? I tried to resolve this by using a repeat loop and pausing the execution of the script until a model with their name is found in the workspace along with their humanoid which seems to have resolved the issue, however, i assume there are more efficient options in assuring the character is accessible/movable and that’s what i’m looking for. Thank you.

game:GetService("Players").PlayerAdded:Connect(function(plr)
	wait()

	leaderboardSetup(plr)

	plr.CharacterAdded:Connect(function(char)
			
			repeat
				wait()
			until
			workspace:WaitForChild(plr.Name) and char:WaitForChild("Humanoid")

			local forcefield = char:WaitForChild("ForceField"):Destroy()
			local leaderstats = plr:WaitForChild("leaderstats")

			if leaderstats.Stage.Value == 0 then

				-- print("No stage value.")
				char:WaitForChild("HumanoidRootPart").CFrame = workspace:FindFirstChild("StartSpawn").CFrame + Vector3.new(0, 4, 0)
				return

			else

				-- print("Player has stage value > 0.")
				char:WaitForChild("HumanoidRootPart").CFrame = game.ServerStorage.CheckpointData[tostring(plr.userId)].Value.CFrame + Vector3.new(0, 4, 0)
				return
		
		end

	end)

end)
2 Likes

try using .CharacterLoaded() instead, it will still return the character the only difference is it won’t until the character is fully loaded. Note that using a StarterCharacter or setting LoadCharacterAppearance to false will never trigger this connection

1 Like

i don’t believe the .CharacterLoaded() event exists, the alternative to that is CharacterAdded which i currently use, but i still have issues, in that the character isn’t properly moved everytime.

I believe I resolved the issue by adding char:WaitForChild("HumanoidRootPart"), after the CharacterAdded event. Similair to what I had first done, but the repeat loop was irrelevant.

1 Like

You should use char:MoveTo(workspace:FindFirstChild("StartSpawn").Position+ Vector3.new(0, 4, 0)) instead.

My intent is to teleport the player by changing their CFrame, rather than having their character walk to the position.

Yeah, that’s what it does, it changes their position just like how a teleport would work. It’s what you’re trying to do but simpler.
You are confusing it with Humanoid:MoveTo() as it walks the humanoid but using MoveTo() on a model (which is char in this case) just teleports it to the position.

1 Like

You’re right, sorry. would char:WaitForChild("HumanoidRootPart") still be relevant after CharacterAdded if i use char:MoveTo()

1 Like

You don’t need char:WaitForChild("HumanoidRootPart) if you’re just using MoveTo(), though you do need to use this connection to assure the character has its primary part ready.

local Players = game:GetService("Players")
Players.PlayerAdded:Connect(function(Player)
	Player.CharacterAppearanceLoaded:Connect(function(Character)
		Character:MoveTo(workspace.Part.Position)
	end)
end)
1 Like

Thank you. I tried Character:MoveTo and CharacterAppearanceLoaded, but I opted to stick with my solution above by adding a wait() for the players root part which has resolved the issue.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.