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

Random brick spawning script broken?

 Part = 1
Wedge = 2
Cylinder = 3	

	while true do
		Instance.new(math.random(1,2,3))
wait(.5)
	end

It's supposed to create a random brick that is either Part, Wedge or cylinder.

Part of an exercise I am doing. Thank you! :D

1 answer

✓ Accepted answer

Good attempt but you have used math.random incorrectly and your variables are useless.

In your code you have told math.random to start at 1, end at 2 and increment by 3 each time. (Which is impossible!) Math.random should be used like this: math.random(starting number, end number, [increment])

Your variables are not getting used in your code so i have replaced them with an array (table)

Below i have changed and fixed your code.

--NilLogic

Objects = { --Define the different objects
"Part",    --Index of 1
"Wedge",  --Index of 2
"Cylinder"   --Index of 3
}

 --[[
objects[1]  would return "Part"
objects[2]  would return "Wedge"
objects[3]  would return "Cylinder"

Instance.new(objects[1])  would create a new part.
Instance.new(objects[2])  would create a new wedge.
Instance.new(objects[3])  would create a new cylinder.

Using math.random we can select one of the above commands to be run
math.random(1,3)  will return either 1, 2 or 3

Which allows us to select an object at random.
objects[math.random(1,3)]  would return either "Part", "Wedge" or "Cylinder"

So finally we can create an object at random by combining all the code together.
Instance.new(objects[math.random(1,3)]) 
--]]

while true do
        Instance.new(objects[math.random(1,3)]) 
	wait(.5)
end

3 comments

0

To add onto this I suggest doing while wait(0.5) do then have an end it just cleans up nicer

0

^

0

^

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