Creating the table
You can either store each position as a Vector3 value, or as a value that is easily converted to a Vector3.
For the sake of understanding, for now lets just put actual Vector3 values inside the table.
local Table = {
Vector3.new(25, 0.5, -27) ,
Vector3.new(25, 0.5, -4)
}
Selecting a random value in the table
We are going to use the relatively new Random datatype in order to get a new number. Consider this a small tutorial.
First we create a new Random object which contains a new state of randomness.
local state = Random.new()
Next, we call the NextInteger on our state, using 1 as our minimum, and the length of our array as our maximum. This will generate a new integer(whole number) within the range of indices on our array.
local randomIndex = state:NextInteger(1, #Table)
Lastly, to get the Vector3, we now index our table with the randomIndex, and it gives us the associated value.
local randomPosition = Table[randomIndex]
Boom! The variable randomPosition now contains a random position drawn from our table.
Teleporting the Character Model
Your current method of CFraming their Head is unneeded, and a bit overkill. Before joints were changed, that would decapitate a character.
Instead, you should use the MoveTo method on their character. This is very simple, and it takes a Vector3 as a single argument.
character:MoveTo(randomPosition)
Getting the Character Model
Instead of using a LocalScript, you should likely be using a Script located in ServerScriptService. The reason for this, is you want everyone to teleport at the same time, and handle it in a central location.
To get everyone's Characters, you should get a current table of all of the current Players, and then if their Character Model exists, teleport them, if not, they are dead or still joining and will have to wait until the next round.
local players = game:GetService("Players")
local function teleportAll(position)
for _, player in pairs(players:GetPlayers()) do
local character = player.Character
if character then
character:MoveTo(position)
end
end
end
Putting it all together
We have all the components now, let's put it all in one Script located in ServerScriptService and throw it in a while loop.
local players = game:GetService("Players")
local state = Random.new()
local Table = {
Vector3.new(25, 0.5, -27),
Vector3.new(25, 0.5, -4)
}
local function teleportAll()
local index = state:NextInteger(1, #Table)
local position = Table[index]
for _, player in pairs(players:GetPlayers()) do
local character = player.Character
if character then
character:MoveTo(position)
end
end
end
while true do
wait(5)
teleportAll(randomPosition)
end
The rest is up to you!
amanda
1 comment
Hm... Whenever I try it out, it says: "Error bad argument #1 to 'new' (Vector3 Expected, got string)