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"]
}
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 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!)
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 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
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
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