# Issue with math.random

Hello!
I don’t necessarily have an issue with a script though how to write it. I’m using math.random to choose a random room (labeled as colors) and move it to a part. I made a table with all of the rooms and the math.random thing but not sure how to make an if statement if a certain room is chosen…

``````
local rooms = {

game.ReplicatedStorage["Blue"],
game.ReplicatedStorage["Red"],
game.ReplicatedStorage["Yellow"] ,
game.ReplicatedStorage["Green"]

}
``````
``````local rando1 = math.random(1, #rooms)
``````

Thanks!

1 Like
``````if rooms[rando1] then
--we found a room!
end
``````

Basically, the square brackets are meant to index a specific element of your rooms list. This if statement checks if that specific color in your list is nill or not, if it isn’t, then the code executes.

This makes a variable of the certain room that was picked

``````local chosenRoom = rooms[rando1]
``````

So like this:

``````local rando1 = math.random(1, #rooms)

if rooms[rando1] then
rando1:Clone()
rando1.Parent = workspace
local move = part1
rando1:MoveTo(move)
end``````
1 Like

I edited my comment, sorry for the confusion

This is the final solution, based on the information you provided me:

``````local chosenRoom = rooms[rando1]
if chosenRoom then
local _chosenRoom = chosenRoom:Clone()
_chosenRoom.Parent = workspace
local move = part1
_chosenRoom.Position = part1.Position
end
``````

In your latest script, you were actually cloning the random number itself. You need to clone your rooms table with the index of your random number. (I stored it in a variable so that it’s easier to read!)

Oh! ok thanks.
I get the error message:
Unable to cast Instance to Vector3

A random color is picked, And it is parented to the workspace but does not move.

1 Like

I’m not sure of what you mean.
If you want to copy a part then do local copied = rando1:Clone()
If you want to change a parts color store “Green” as a string and store colors as color3 variables

local green = Color3.fromRGB(0, 255, 0)

local rooms = {
“Green”,
“Blue”,
“Red”
}

if math.rando1 == “Green” then
part.Color = green

also good to see you’re active

No uh green is the name of the room

This involves CFrames let me go get my code to look at,

1 Like

Ok thanks
here is the full code for refrence:

``````
local rooms = {

game.ReplicatedStorage["Blue"],
game.ReplicatedStorage["Red"],
game.ReplicatedStorage["Yellow"] ,
game.ReplicatedStorage["Green"]

}

local part1 = workspace.Location1
local part2 = workspace.Location2
local part3 = workspace.Location3
local part4 = workspace.Location4

local rando1 = math.random(1, #rooms)

local chosenRoom = rooms[rando1]
if chosenRoom then
chosenRoom:Clone()
chosenRoom.Parent = workspace
local move = part1
chosenRoom:MoveTo(move)
end
``````

Are the rooms identical except for color

The rooms are not identical. Each room is the same shape but have a different color block on them

1 Like

Alright, I just made a blank studio project and tried the code. Found the error, you forgot to store the cloned part in a separate variable. This works:

``````local rooms = {

game.ReplicatedStorage["Blue"],
game.ReplicatedStorage["Red"],
game.ReplicatedStorage["Yellow"] ,
game.ReplicatedStorage["Green"]

}

local part1 = workspace.Location1
local part2 = workspace.Location2
local part3 = workspace.Location3
local part4 = workspace.Location4

local rando1 = math.random(1, #rooms)

local chosenRoom = rooms[rando1]
if chosenRoom then
local _chosenRoom = chosenRoom:Clone()
_chosenRoom.Parent = workspace
--local move = part1 This line is unnecessary!
_chosenRoom.Position = part1.Position
end
``````

Erm the rooms are models. .postion = .position does not work.

In your model you will see a attribute called primarypart, make a part called hitbox or TotalPosition in the direct center of your part.

You will then need to change the position of this part

Divide the models size by
X / 2
Y / 2
Z / 2
and make the parts position that for now, you will then need to add to it but ill work that out right now

``````local rooms = {

game.ReplicatedStorage["Blue"],
game.ReplicatedStorage["Red"],
game.ReplicatedStorage["Yellow"] ,
game.ReplicatedStorage["Green"]

}

local part1 = workspace.Location1
local part2 = workspace.Location2
local part3 = workspace.Location3
local part4 = workspace.Location4

local rando1 = math.random(1, #rooms)

local chosenRoom = rooms[rando1]
if chosenRoom then
local _chosenRoom = chosenRoom:Clone()
_chosenRoom.Parent = workspace
--local move = part1 This line is unnecessary!
_chosenRoom:MoveTo(part1.Position)
end
``````
1 Like

HAZZAH!!! It works!!!

1 Like

that too works I forgot about the MoveTo function, thanks Abigail. Good luck marshmellow

2 Likes