Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

CFrame teleport script not teleporting players?

Hi! I have a teleporting script that moves players from their current position to a random choice of set positions on the map. I have recently condensed a bunch of messy if-statements to a for-loop, but now the script does not teleport. It says that it did, but nothing happened. Here is one of the for-loops that I have, plus the function that they are inside:

local function tpPlayers()
	local players = game.Players:GetPlayers()
	local playerSpawns = math.random(1,8)

	if mapChosen == "MapOne" then
		for i = 1,1,8 do
			if playerSpawns == i then
				players[1].Character.HumanoidRootPart.CFrame = CFrame.new(game.Workspace["MapOne"].Spawns:GetChildren()[i].Position)
			end
		end
	end
end

If you're wondering what Spawns:GetChildren()[i] is, I have a folder with the map name on it. Inside of that, I have a folder with the name Spawns. Inside of that, I have parts with names Spawn1 through Spawn8.

If you have any questions, please feel free to ask them. I hope you can help, and if you can, thank you! :)

P.S. I will feel very stupid if I made a typo in something...

Edit: I have recently discovered that the error is coming from the fact that mapChosen is apparently not equal to any of the map names. It shows that it is in the output, but it still doesn't work.

1 comment

0

for loop's syntax is for init,min/max,increment so you might want to try to make it for i = 1,8,1

2 answers

✓ Accepted answer

well, there's a bugs in your code.. The bug is with the for loop. the syntax for a for loop is as follows: for variable = startingNumber, NumberToGoUpTo, increment do ... end , but your for loop is like this: for variable = startingNumber,increment , NumberToGoUpTo do ... end .

which means, the for loop won't run at all because the condition is already met..

here is your code re-written.

local Players = game:GetService("Players")
function TeleportPlayers()
	if(MapChosen == "MapOne" then
		local spawns  = workspace["MapOne"].Spawns:GetChildren();
		local players = Players:GetPlayers();

		for _, player in pairs(players) do
			local spawnIndex = math.random(1,#spawns)
			local spawn = spawns[spawnIndex];
			local char = player.Character 
			char.HumanoidRootPart.CFrame = spawn.CFrame
		end
	end
end

I cannot fix the loop itself but I can say there is a flaw in your script with the teleporting.

You are using

Character.HumanoidRootPart.CFrame

And not

Character:SetPrimaryPartCFrame()

This will totally fix your teleportation hopefully!

Example

players[1].Character:SetPrimaryPartCFrame(CFrame.new(game.Workspace["MapOne"].Spawns:GetChildren()[i].Position))

Edit: If you're getting an error with teleportation saying to change the CFrame to Vector3 then listen

3 comments

0

actually, its the same thing..

0

Well I much perfer that instead of grabbing the HumanoidRootPart directly as I got buggy results by doing that

0

i mean in some cases, like when the parts are not connected like a character is, this is actually the right way to do it, and the sole reason why roblox made the method. so my previous comment is actually wrong

This discussion is preserved as it was. New answers, comments, and votes are closed.