Scripting help needed (math.random, for loop, array, folder)

Hello!

  1. I wanted to assign blocks to specific positions using this script.

  2. I don’t know how to write function and for loop to iterate by each block.

  3. I tried fixing it but i didn’t work

local RS = game:GetService("ReplicatedStorage")


local onecf = workspace.FolderOfpos.one.Position --pos fold
local twocf = workspace.FolderOfpos.two.Position
local threecf = workspace.FolderOfpos.three.Position
local fourcf = workspace.FolderOfpos.four.Position

local meshfolder = RS.Blocks
local arrayOfCFs = {
	onecf,
	twocf,
	threecf,
	fourcf
}


local a = math.random(1, #arrayOfCFs)

print(a)

local b = meshfolder.oneblock
b.Position = arrayOfCFs[a]

table.remove(arrayOfCFs, a)

local c = math.random(1, #arrayOfCFs)

print(c)

local d = meshfolder.twoblock
d.Position = arrayOfCFs[c]

table.remove(arrayOfCFs, c)

local e = math.random(1, #arrayOfCFs)

print(e)

local f = meshfolder.threeblock
f.Position = arrayOfCFs[e]

table.remove(arrayOfCFs, e)

local g = math.random(1, #arrayOfCFs)

print(g)

local h = meshfolder.fourblock
h.Position = arrayOfCFs[g]

table.remove(arrayOfCFs, g)


for _, block in pairs(meshfolder:GetChildren()) do
	block:Clone().Parent = workspace
end

To simplify, rename the parts to numbers like 1, 2, 3, 4.
Loop through the cframes like so:

for index, cframe in arrayOfCFs do

end

inside the loop find the appropriate part from the index 1-4 and set its frame:

workspace.FolderOfpos[tostring(index)].CFrame = cframe

Edit: remember to convert indexer to a string.

1 Like

Thank you for help! I’ll try it in a moment.

It gave me an idea and I did it! Thank you again! It works now.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.