I'm trying to make a Weapon Royale game, and I am making the game teleport players after 5 seconds, to a random spawnpoint within the map. I am wondering if someone could spread some info on how to use math.random() with a table of numbers or a string value? Here's what I tried that isn't working:
local Table = {"25, 0.5, -27" , "25, 0.5, -4"} while true do wait(5) game.Workspace:WaitForChild(game.Players.LocalPlayer.Name).Head.CFrame = CFrame.new(math.random(table)) end
Any help?
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) }
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.
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)
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 -->iterate through all current players local character = player.Character if character then -->check if character exists character:MoveTo(position) -->teleport to given position end end end
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) -->random index local position = Table[index] -->random position for _, player in pairs(players:GetPlayers()) do local character = player.Character if character then character:MoveTo(position) -->actual teleporting end end end while true do wait(5) teleportAll(randomPosition) end
The rest is up to you!
I don't know if your table is meant to have two strings that encompass commas, but you're best bet is to do this
local Table = {"25, 0.5, -27" , "25, 0.5, -4"} while true do wait(5) game.Workspace:WaitForChild(game.Players.LocalPlayer.Name).Head.CFrame = CFrame.new(Table[math.random(#Table)]) end
Table[math.random(#Table)]
All this does is take the amount of keys in the table, and picks a random key. The Table[] takes the key randomly generated from math.random(#Table). This is your best.
Sorry if my explanation isn't the best, I just woke up lol. Hope this helps.
Your problem is that you cannot use strings in CFrames and you're trying to give a table to math.random()
but math.random()
accepts 2 numbers. Try this:
local Table = {CFrame.new(25, 0.5, -27) , CFrame.new(25, 0.5, -4)} while true do wait(5) workspace:WaitForChild(game.Players.LocalPlayer.Name).Head.CFrame = Table[math.random(#Table)] end
Hope I helped
-ThisIsAnAccount255