Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Randomly selecting a block to unanchor?

My current code creates a 6x5 grid of tiles, with an array that stores each brick in the grid. My question is, how could I make it so a randomly selected brick is unanchored? I have a random number 1-30, but I'm not sure how I can use that random value to select the brick I want to unanchor.

for i=1,30, 1 do
	Brick = Instance.new("Part")
	Brick.Name = i
	brickArray = {}
	table.insert(brickArray, i)
	Brick.Parent = workspace
	Brick.Anchored = true
	Brick.Size = Brick.Size + Vector3.new(20,0,22)
	Brick.BrickColor = BrickColor.Random()
	if i < 6 then
		Brick.CFrame = CFrame.new(24*i,0,0)
		elseif i < 12 then
		Brick.CFrame = CFrame.new(24*(i-6),0,24)
		elseif i < 18 then
		Brick.CFrame = CFrame.new(24*(i-12),0,48)
		elseif i < 24 then
		Brick.CFrame = CFrame.new(24*(i-18),0,72)
		elseif i < 30 then
		Brick.CFrame = CFrame.new(24*(i-24),0,96)
	end
	print(brickArray[1])
end

local random = math.ceil(math.random(0,30))

2 answers

✓ Accepted answer

Ok, most of that code you have is quite unnecessary. You should make it all with one parent. Then after that write this in a normal script.

local Parent = game.Workspace.Tiles --- Here you write where all the tiles are
local children = Parent:GetChildren()
for i = 1,30 do
	local randnum = math.random(1,30)
	for i,v inPairs(children) do --- A script that goes through all the children of your parent
		if i = randnum then
			v.Anchored = false --- V is representing the current child it is on.
		end
	end
	wait(3)
end

Hope this helps!

5 comments

0

This worked after I corrected a few typos. Could I message you over Roblox or Discord or something though? I'm a little confused on how the top half actually works. Also, how do I clear up my unnecessary code?

0

K

0

my discord is greenhamster1

0

#8993

0

So basically you don't have much unnecessary code, you just should always look at easier methods to do things.

You can use the "math.random()" function to get a random address inside the table, how like this:

--> Made by Convexyz

local tab = {}; --> Your brick Table.

local selected = tab[math.random(1, #tab)];
selected.Anchored = false;

4 comments

0

yours seems easier, but how will he reference the part from a table. I am confused about that part. I am not really good with tables.

0

I tried that, but it's throwing me an error still. Here's what I wrote: local selected = brickArray[math.random(1, #brickArray)]; selected.Anchored = false

0

Whats the error.

0

I think @GarryGecko99 is trying to reach a table what is inside a loop, so it won't work.

This discussion is preserved as it was. New answers, comments, and votes are closed.