First: Indent and space your code properly!
Addressing some problems:
game.Players() is not a function. To get all of the players, use game.Players:GetPlayers().
Use :FindFirstChild not :findFirstChild (it's deprecated).
Use workspace instead of game.Workspace.
Get rid of that useless do ... end in there, too.
You really shouldn't be changing v, and you should use better names. v and c are bad names, I don't know what a "v" is.
function _G.GetPlayer()
local tppoints = workspace["map tp"]:GetChildren()
local randomSelect = math.random(1, #tppoints)
local randomTp = tppoints[randomSelect]
local players = game.Players:GetPlayers()
for _, player in pairs(players) do
local character = players.Character
local torso = character:FindFirstChild("Torso")
for _, c in pairs(randomTp) do
torso.CFrame = CFrame.new(c.Position)
end
end
end
Let's go over the types of the variables you have.
What i mean by "type" is what you are allowed to do with an object.
For example, a Player object has a .Character, which has a .Humanoid, which has a .Health. But a Player itself doesn't have a .Health; it's a different type of thing.
Similarly, workspace is an object; I can use :GetChildren() on it. The result of workspace:GetChildren() is a list of objects. I can use list[1] or #list or pairs(list), and these things give me different objects.
Let's write the types of all of the objects in your script.
function _G.GetPlayer()
local tppoints = workspace["map tp"]:GetChildren()
local randomSelect = math.random(1,#tppoints)
local randomTp = tppoints[randomSelect]
local players = game.Players:GetPlayers()
for _, player in pairs(players) do
local character = players.Character
local torso = character:FindFirstChild("Torso")
for _, c in pairs(randomTp) do
torso.CFrame = CFrame.new(c.Position)
end
end
end
There are two problems here.
- You use
players.Character, but players is a list, not a player. You mean to use player.Character
- You use
pairs(randomTp), but randomTp isn't a list, it's a brick.
I'll simplify your code to just use randomTp instead where you had c to solve (2).
function _G.GetPlayer()
local tppoints = workspace["map tp"]:GetChildren()
local randomSelect = math.random(1,#tppoints)
local randomTp = tppoints[randomSelect]
local players = game.Players:GetPlayers()
for _, player in pairs(players) do
local character = player.Character
local torso = character:FindFirstChild("Torso")
torso.CFrame = CFrame.new(randomTp.Position)
end
end
There's a small problem here. torso can be nil (since it's the result of FindFirstChild), but you try modifying it.
Since it's possible the torso isn't there, you need to check for that case
local torso = character:FindFirstChild("Torso")
if torso then
torso.CFrame = CFrame.new(randomTp.Position)
end
In fact, you should also have a check that character isn't nil.
This script probably doesn't do what you want, though. It does this:
- Pick a random spawn,
randomTp
- Teleport each player to that one
randomTp spawn.
You probably want the random choice to be done for each player. But right now, it's not done inside the for loop, so it's not done for each.
Clean Code
Clean code reads like well-written prose.
Each part is very compact, understandable, and says exactly what you do.
Notice how the description / purpose of what the function does reads almost exactly like the code inside it?
function teleport(player, target)
local character = player.Character
if character then
local torso = character:FindFirstChild("Torso")
if torso then
torso.CFrame = CFrame.new(target.Position)
end
end
end
function chooseRandomElement(list)
return list[math.random(#list)]
end
function randomSpawn()
return chooseRandomElement(
workspace["map tp"]:GetChildren()
)
end
function teleportPlayers()
for _, player in pairs(game.Players:GetPlayers()) do
teleport(player, randomSpawn())
end
end
2 comments
Thank you so very much, very informative, and I will try to keep my code clean!
Unfortunately it still does not work, but im going to accept your post as the answer considering Its as close to the answer as I am going to get I believe.