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.
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