I am trying to make a script that will teleport players to a random 'spawn' location within a map. The 'map' variable is already defined earlier in the script. (This is a chunk of my larger script)
01 | local MapPlayerSpawns = map.SpawnLocations:WaitForChild( "PlayerSpawns" ):GetChildren() |
02 | function tp() |
03 | local Players = game.Players:GetPlayers() |
04 | for i,v in pairs (Players) do |
05 | local randomPTP = math.random( 1 ,#MapPlayerSpawns) |
06 | local IndSpawn = MapPlayerSpawns [ randomPTP ] |
07 | wait( 0.1 ) -- For loading times |
08 | local plr = Players [ i ] |
09 | plr:WaitForChild( "Character" ):SetPrimaryPartCFrame(IndSpawn.CFrame + Vector 3. new( 0 , 5 , 0 )) -- Script works but doesn't teleport the player. |
10 | end |
11 | end |
12 | tp() |
Note: This isn't a duplicate question, I asked a different question with a similar script earlier.
I hope you can help me.
I believe "Character" is a property and not a child of the player object. Also you're looping through a list of players so you don't need to do Players[i] or anything like that to get the player.
Lets see if I can fix this for ya.
01 | local MapPlayerSpawns = map.SpawnLocations:WaitForChild( "PlayerSpawns" ):GetChildren() |
02 | function tp() |
03 | local Players = game.Players:GetPlayers() |
04 | for i,v in pairs (Players) do |
05 | local randomPTP = math.random( 1 ,#MapPlayerSpawns) |
06 | local IndSpawn = MapPlayerSpawns [ randomPTP ] |
07 | wait( 0.1 ) -- For loading times |
08 | if (v.Character) then |
09 | v.Character:SetPrimaryPartCFrame(IndSpawn.CFrame + Vector 3. new( 0 , 5 , 0 )) -- Script works but doesn't teleport the player. |
10 | end |
11 | end |
12 | end |
13 | tp() |
So I just check if the player currently has a character and if they have it, teleport them.
Just Use the Character's Humanoid Root Part, instead of wasting your energy writing out The Primary Part CFrame!
And Also as @CeramicTile Said, the Character is not a property of the Player lol! <:D>!
----T40! ,2019
<:D>!