Teleporting player to a part inside of a for loop

Trying to get the player to teleport to the part, doesn’t error or do anything.

for i = 1,#playerspawns do
        if playerspawns[i].Taken.Value == false then
            playerspawns[i].Taken.Value = true
            char.HumanoidRootPart.CFrame = CFrame.new(playerspawns[i].Position)
            break
        else
            print("too bad")
        end
    end

Have you tried SetPrimaryPartCFrame? Maybe, for some reason, CFraming the HumanoidRootPart isn’t moving the character.

Keep in mind the primary part of the character is usually the head (IIRC). So you might want to do CFrame.new(playerspawns[i].Position) * CFrame.new(0, 6, 0).

Additionally, try adding a print before the break to see if the CFrame is actually being set. Make sure your code is executing before you get confused about why it isn’t doing anything. :stuck_out_tongue:

Yeah, none of that worked. :confused:

None of what worked? If you add a print statement then does it print to console?

If it doesn’t then your code isn’t actually executing correctly…

I’m confused, is playerspawns an instance or a dictionary?

Yes it prints to my console, also isn’t SetPrimaryPartCFrame only used for models?

The player character is a model

Aren’t characters models too…?

Alright, so I’ve tried moving the character model, and it’s saying “Unable to cast Vector3 to CoordinateFrame”

That means you passed a Vector3 where a CFrame should be

Yeah, so I can’t use SetPrimaryPartCFrame. I was doing it correctly the first time.

Excuse me?

char:SetPrimaryPartCFrame(CFrame.new(playerspawns[i].Position) * CFrame.new(0, 6, 0))
NOT char:SetPrimaryPartCFrame(playerspawns[i].Position)

This is user error.

Yeah, the character hasn’t moved.

An instance, local playerspawns = workspace:WaitForChild(“PlayerSpawns”):GetChildren()

i tested your code in a example setup and From everything that i can see the snippet you shared should work and if the print that you placed above break prints, then i’m not sure why it isn’t working . which leads me to believe something else is to blame, Could you possibly provide more of your script?

This is the entire script.

local playerspawns = workspace:WaitForChild("PlayerSpawns"):GetChildren()

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(char)
		player.Character.Humanoid.WalkSpeed = 0
		player.Character.Humanoid.JumpPower = 0
	for i = 1,#playerspawns do
		if playerspawns[i].Taken.Value == false then
			playerspawns[i].Taken.Value = true
			char.HumanoidRootPart.CFrame = CFrame.new(playerspawns[i].Position)
			print("hello mate")
			break
		else
			print("too bad")
		end
	end
	end)
end)

i figured it out i think, it’s not working because you are trying to set the character’s cframe too early, so try yeilding/waiting before setting char.HumanoidRootPart.CFrame

1 Like

Yeah, that was exactly it. Thank you a lot!

1 Like

Don’t forget to mark their post as the solution if it solved your problem.