Why won't my character teleport?

So I have a script set up for teleporting a player after the map has loaded, except they won’t teleport. If there is any way you can help me fix this that would be great, I have nothing that comes up in the output after it goes through the for loop sadly, so something is wrong with the loop itself I believe. Could this be a bug, or do I just not have something made correctly? Here is part of my script:

-- define
local currentmap = workspace:WaitForChild("CurrentMap")
local chosenmap = script:WaitForChild("ChosenMap") -- this is set when the map has been chosen
local players = game.Players:GetPlayers()

-- teleport players
function teleportPlayers()
	for i,v in pairs(players) do
		v.Character.HumanoidRootPart.CFrame = currentmap:FindFirstChild(chosenmap.Value).Spawn.CFrame * CFrame.new(math.random(5,10),0,math.random(5,10))
	end
end

Thanks For Reading! Any Help Is Very Appreciated! :slightly_smiling_face:

2 Likes

Can you try calling MoveTo() on the player’s character?

The issue is probably because of the local players = game.Players:GetPlayers() line, because the table assigned to this variable will always be constant, unless you’re actually updating it somewhere else, but in this excerpt, it’s not being updated and also perform checks to make sure that their character exists so the script doesn’t throw any errors

You should define a new variable when the function is called:

function teleportPlayers()
    local players = game.Players:GetPlayers()
    for _,v in pairs(players) do
        if v.Character then
            -- adjust HRP CFrame here
        end
    end
end
2 Likes

are u using filtering enabled? or you just doin all this inside local script?

no, none of this is a local script, I am using a (Server)Script to call my functions. That wouldn’t make sense if I did this in a local script and yes I am using filtering enabled.

MoveTo() doesn’t teleport the player, it makes the character walk to the place.

1 Like

That’s if it’s called on the humanoid, if it’s on the model it will teleport

That’s only if MoveTo() is called on a humanoid. When it is called on a model, the model will teleport to the desired position.

Edit: Exactly what C0lvy123 said.

1 Like

oh yeah I could do that, but the problem was the server not constantly assigning the “players” variable, as @Raretendoblox has said. I just tested this out, but thanks for the help, I appreciate it! :slightly_smiling_face:

1 Like