ok so I have the script that choose a random place within the model and teleports it there. The parts in the model are all named "StartingPlaceOne-Twelve" how would I make it so it's not random and teleports the first player to the first one and the second player to the second one etc...
--Here is the global table _G.StartingPlaces = {"StartingPlaceOne","StartingPlaceTwo","StartingPlaceThree","StartingPlaceFour","StartingPlaceFive","StartingPlaceSix","StartingPlaceSeven","StartingPlaceEight","StartingPlaceNine","StartingPlaceTen","StartingPlaceEleven","StartingPlaceTwelve"} Spawns = game.Workspace:WaitForChild("Spawns") Players = game.Players:GetPlayers() --Define players for PlayerTeleport = 1, #Players do if Players[PlayerTeleport]:WaitForChild("AFK").Value == true then local char = Players[PlayerTeleport].Character --Essentially waits for the CharacterAdded event to fire, then returns the character. char:WaitForChild("Torso").CFrame = Spawns[_G.StartingPlaces[math.random(1,#_G.StartingPlaces)]].CFrame * CFrame.new(0,8,0) --here end end
The problem lies in line 11. You don't have to use the math.random()
function to teleport players. In fact, it's kind of redundant.
Refer to line 8. PlayerTeleport
. It's the number of the current iteration of the loop. You can access the global table with this value.
char:WaitForChild("Torso").CFrame = Spawns[_G.StartingPlaces[PlayerTeleport]].CFrame * CFrame.new(0,8,0)
--Here is the global table _G.StartingPlaces = {"StartingPlaceOne","StartingPlaceTwo","StartingPlaceThree","StartingPlaceFour","StartingPlaceFive","StartingPlaceSix","StartingPlaceSeven","StartingPlaceEight","StartingPlaceNine","StartingPlaceTen","StartingPlaceEleven","StartingPlaceTwelve"} Spawns = game.Workspace:WaitForChild("Spawns") Players = game.Players:GetPlayers() for PlayerTeleport = 1, #Players do if Players[PlayerTeleport]:WaitForChild("AFK").Value then -- You don't need the "== true"; the "if" statement itself checks if its condition is true. local char = Players[PlayerTeleport].Character char:WaitForChild("Torso").CFrame = Spawns[_G.StartingPlaces[PlayerTeleport]].CFrame * CFrame.new(0,8,0) end end
Want to shrink your script? You can use :SetPrimaryPartCFrame() to move your character. Also, the script could break on line 10, because you don't really know if the Character is nil
or not. You can either wait until it isn't nil
, or just skip the player.
char:SetPrimaryPartCFrame(Spawns[_G.StartingPlaces[PlayerTeleport]].CFrame * CFrame.new(0, 8, 0))
Wait for the character.
local char = Players[PlayerTeleport].Character or Players[PlayerTeleport].CharacterAdded:wait()
Skip over the player.
local char = Players[PlayerTeleport].Character if char then char:SetPrimaryPartCFrame(Spawns[_G.StartingPlaces[PlayerTeleport]].CFrame * CFrame.new(0, 8, 0)) end