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

The math.random() is not working properly?

Asked by 5 years ago
Edited 5 years ago

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?

0
your probably making an thing where tiles disappear making players fall but math.random can be used in numbers you know cherrythetree 130 — 5y
0
#Tile returns a number. It's the length operator for a string / table. Fifkee 2017 — 5y
0
im assuming that your code will ONLY destroy one item. Am i right? XviperIink 428 — 5y
0
Yeh robloxsario 76 — 5y
View all comments (2 more)
0
i know the answer at this web. https://scriptinghelpers.org/questions/26306/random-event-then-destory-not-working .sorry for inconvenience robloxsario 76 — 5y
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()". DeceptiveCaster 3761 — 5y

1 answer

Log in to vote
0
Answered by 5 years ago
Edited by User#24403 5 years ago

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

```lua 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 ```

Ad

Answer this question