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)
local MapPlayerSpawns = map.SpawnLocations:WaitForChild("PlayerSpawns"):GetChildren() function tp() local Players = game.Players:GetPlayers() for i,v in pairs(Players) do local randomPTP = math.random(1,#MapPlayerSpawns) local IndSpawn = MapPlayerSpawns[randomPTP] wait(0.1) -- For loading times local plr = Players[i] plr:WaitForChild("Character"):SetPrimaryPartCFrame(IndSpawn.CFrame + Vector3.new(0, 5, 0)) -- Script works but doesn't teleport the player. end end 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.
local MapPlayerSpawns = map.SpawnLocations:WaitForChild("PlayerSpawns"):GetChildren() function tp() local Players = game.Players:GetPlayers() for i,v in pairs(Players) do local randomPTP = math.random(1,#MapPlayerSpawns) local IndSpawn = MapPlayerSpawns[randomPTP] wait(0.1) -- For loading times if (v.Character) then v.Character:SetPrimaryPartCFrame(IndSpawn.CFrame + Vector3.new(0, 5, 0)) -- Script works but doesn't teleport the player. end end end 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>!