I have a script that picks a random position on the map. in it there is a list of usable positions:
function pickrandomspawnpoint()
math.randomseed(tick()%1*1e6)
local choose = math.random(1,5)
local part = script.Parent
local pos1 = part.Position
local pos2 = part.Position + Vector3.new(10,0,0)
local pos3 = part.Position + Vector3.new(20,0,0)
local pos4 = part.Position + Vector3.new(30,0,0)
local pos5 = part.Position + Vector3.new(40,0,0)
end
while true do
wait(10)
pickrandomspawnpoint()
end
I cant seem to figure out how to pick a random value listed above.
I wanted to do something like this:
local numberindex = 1 --the number at the end of each "pos" value--
local pickedval = "Pos"..numberindex --the current "pos" value--
is there a way to use this kind of method on local values? I don’t want to create a list of Stringvalues inside a folder as I want everything to be taken care of in this one script.
You don’t need to iterate through the table, you just have to index a random number starting from 1 and ending with the table length like so:
local Spawns = {Vector3.new(1,2,3), Vector3.new(4,5,6), Vector3.new(7,8,9)}
local function GetRandomValue(tbl)
return tbl[Random.new:NextInteger(1, #tbl)]
end
print(GetRandomValue(Spawns)) -- Will return a random value from the table, spawns
Then to incorporate this into your script:
while true do
wait(10)
script.Parent.Position = GetRandomValue(Spawns)
end
any way to fit in the random seed? I use it along with math.random because it tends to pick the same thing over and over in the studio without it, unless roblox fixed this?
I need to know what type of index you’re using, I’m assuming it’s an array, if that’s the case, then here’s what you do:
local positions = {Vector3.new(0,0,0), Vector3.new(1, 2, 3)} -- table looks like this
local random = Random.new() -- the roblox random class, it's more random.
local positionIndex = random:NextInteger(1, #positions) -- get the index of the table
local newPosition = positions[positionIndex] -- reference that value of the table
Or if it’s a dictionary, you’d do the same thing, except you’d add the key to a table and chose a random key and then index the value.
Only do this if your table looks like this local Table = {Noob = "Hi"}
local positions = {Position1 = Vector3.new(1, 2, 3), Position2 = Vector3.new(4, 5, 6)}
local keyTable = {} -- use to store the "Position[#]" key. Looks like this: {"Position1", "Position2"}
for key, _ in pairs(positions) do -- there's no point in storing the value
table.insert(keyTable, key) -- prepping for random selection
end
local random = Random.new() -- the roblox random class, it's more random.
local index = keyTable[random:NextInteger(1, #keyTable)]
local positionIndex = keyTable[index] -- get the index of the table
local newPosition = positions[positionIndex] -- reference that value of the table
The first argument of Random.new is the seed. You should be able to call it with your own seed. You can read more about the random library here: Random | Documentation - Roblox Creator Hub
also, there seems to be an error in your script, line 20, attempting to index field ‘new’ a function value
here is the script I modified:
local part = script.Parent
local pos1 = part.Position
local pos2 = part.Position + Vector3.new(10,0,0)
local pos3 = part.Position + Vector3.new(20,0,0)
local pos4 = part.Position + Vector3.new(30,0,0)
local pos5 = part.Position + Vector3.new(40,0,0)
local Spawns = {pos1,pos2,pos3,pos4,pos5}
local function GetRandomValue(tbl)
return tbl[Random.new:NextInteger(1, #tbl)]
end
while true do
wait(1)
print(GetRandomValue(Spawns))
end
Why don’t you just pick a random position by doing something like this :
associate positions as values with each index, so there would be many ways to do what you want.
Positions = {
[1] = Vector3.new(10,0,0);
[2] = Vector3.new(20,0,0);
[3] = Vector3.new(30,0,0);
[4] = Vector3.new(40,0,0);
}
local ChosenPosition
local randomNumber = math.random(1,#Positions)
print(randomNumber)--maximum number will be 4
ChosenPosition = Positions[randomNumber]
part.Position = part.Position + ChosenPosition --or something
@mantorok4866, Here’s the issue, math.random is outdated, Random.new() is far newer and is much more random. I HIGHLY encourage you to switch to the new random class.
This post is old and the knowledge presented in this thread isn’t as certain - you can see by the way I offer up my responses. I’ve since come to better understand the difference between math.random and Random.new. Reference some of my newer responses for proper information.