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

The math.random() is not working properly?

This is my problem. I made an script that makes the tiles disappear randomly but when I used the math.random(), I not working. I put some parts that named Tile there are 350 Parts named Tile that Part needs to be destroy in random. So the Parent of the Tile are Tiles which is model. The parent of the model is a Model named RandomDestoyedTiles. I put a script under the Model named RandomDestoyedTiles. I put this lines of the code.`

local Model = script.Parent
local Tiles = Model.Tiles
local Tile = Tiles:GetChildren()
local Tilecopy = Tile[math.random(1,#Tile)]
while true do
Tilecopy:Destroy()
wait(3)
end

Is this correct? I mean Do I have typed wrong code. I have make my own lines to think what happening so i used math.random() and while true do. But why i not worked, can you tell me what happen , if worked it is only one Part destroyed.No errors at the output.Why?

6 comments

0

your probably making an thing where tiles disappear making players fall but math.random can be used in numbers you know

0

#Tile returns a number. It's the length operator for a string / table.

0

im assuming that your code will ONLY destroy one item. Am i right?

0

Yeh

0

i know the answer at this web. https://scriptinghelpers.org/questions/26306/random-event-then-destory-not-working .sorry for inconvenience

0

You know that you can make lines 1, 2 and 3 one line, right? It would look like "local Tile = script.Parent.Tiles:GetChildren()".

1 answer

✓ Accepted answer

You're only picking one object once, then repeatedly trying to destroy the same object that was already picked. Try this:

local Model = script.Parent
local Tiles = Model.Tiles
    
while true do
	local children = Tiles:GetChildren() -- Avoid errors when using math.random on an old table just in case.
	if #children > 1 then
		local TileCopy = Tile[math.random(1, #children)]
		TileCopy:Destroy()
	else
		--// No more children to destroy
		print("No more children to destroy")
	end
	wait(3)
end

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