Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Why does this script doesn't clone? Is it because I am not using a for loop?

Asked by 5 years ago

If one of the blocks are missing than this should clone the block. I manually :Destroy()-ed one of the blocks but it didn't generate. !Indented Image The Indented one is up there and the one in Lua Script(this is all 'cause #StackEdit) is over here

local block = {

"Lucky Block1",

"Lucky Block2",

"Lucky Block3",

"Lucky Block4",

"Lucky Block5",

"Lucky Block6",

"Lucky Block7",

"Lucky Block8",

"Lucky Block9",

"Lucky Block10",

"Lucky Block11",

"Lucky Block12",

"Lucky Block13",

"Lucky Block14",

"Lucky Block15",

"Lucky Block16",

"Lucky Block17",

"Lucky Block18",

"Lucky Block19",

"Lucky Block20",

}

local num = 1

while true do

print(block[num])

if not workspace.Blocks:FindFirstChild(block[num]) then

local serverstorage = game:GetService("ServerStorage")

local block = serverstorage:WaitForChild("Lucky Block")

local clone = block:Clone()

clone.Parent = game.Workspace.Blocks

clone.Name = "Lucky Block"..num

clone:MoveTo(Vector3.new(math.random(-253.171,261.659),6,math.random(-513.762,514.205)))

end

if num == 20 then

num = 0

end

num = num + 1

wait()

end

\ Thanks

0
P.S. I don't know how to use the for loop tree_tree00 48 — 5y

1 answer

Log in to vote
1
Answered by 5 years ago

Because you don't know how to use a for loop, I'll show you how.

For this scenario, you'll need a for loop, and you have the choice of either a generic or numeric for loop. A generic for loop is a for loop that iterates through a table using an iterator function, such as pairs() or ipairs(). When to use these iterator functions gets easier as you get familiar with them. pairs() should be used when the table given to its argument has a dictionary part (not all tables have dictionary parts as you have to make it a dictionary). (A dictionary is something like {["food"] = "good"}.) ipairs() should be used when the table is only an array (an array is something like {1, 2, 3}).

Because block is an array, but you appear to be iterating using numbers, you should use a numeric for loop:

for i = 1, #block do -- Runs for the amount of times equal to the table's length
    print(block[i])
end

The above code is just an example. A numeric for loop uses the given loop variable to iterate through a given set of numbers. In the above example, the variable i is iterating from 1 to #block. The set of numbers has a third, optional number that determines the number that the loop uses to iterate through that set of numbers. Example:

for i = 1, #block, 5 do
    print(block[i])
end

The 5, which is the third number in the loop, tells the loop to iterate through the set of numbers by 5's, reading every fifth number in the set. Making this number 2 would go by 2's, reading every second number, 3 would go by 3's, reading every third number, and so on. By default, if you don't give the loop a third parameter, the third parameter automatically becomes 1, so it will iterate through every number in the set.

And that is how you use for loops.

Ad

Answer this question