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

Simple Math.random error I can't fix?

Asked by 8 years ago
while true do
    randround = math.random(1,1)
    wait(1)
    if randround == 1 and game.ReplicatedStorage.NumberOfModels.Value < 220 then
            for f = 1,15 do
                wait(.5)
        for _,od in pairs(Lighting.ODers:GetChildren()) do
                randod = (od[math.random(#od)])
            randod.Parent = workspace
            randod:MoveTo(workspace:WaitForChild("EnabledSpawnBrick").Position + Vector3.new(0,10,0)) 
            randod:MakeJoints()
            end
        end
        timer()
        msg:Destroy()

    end
local pickzom = math.random(1,3)
            wait(.5)
        if pickzom == 1 then
        zh = Lighting.Zombie:clone()
        zh.Parent = workspace
        zh:MoveTo(workspace:WaitForChild("EnabledSpawnBrick").Position + Vector3.new(0,10,0))
        zh:MakeJoints()
0
There are more than one thing wrong with this. What are you trying to do? Lacryma 548 — 8y
0
Randomly select one child out of a model and clone it. laughablehaha 494 — 8y

3 answers

Log in to vote
1
Answered by 8 years ago

If you want a random clone from that just do

Model = blah
clone = Model:GetChildren()[math.random(1,#Model:GetChildren())]:Clone()
Ad
Log in to vote
0
Answered by 8 years ago
while true do
    randround = math.random(1,1)
    wait(1)
    if randround == 1 and game.ReplicatedStorage.NumberOfModels.Value < 220 then
            for f = 1,15 do
                wait(.5)
        for number,od in pairs(game.Lighting.ODers:GetChildren()) do -- forgot datamodel
                randod = (Lighting.ODers:GetChildren()[math.random(number)]) -- don't even want to explain this...
            randod.Parent = workspace
            randod:MoveTo(workspace:WaitForChild("EnabledSpawnBrick").Position + Vector3.new(0,10,0)) 
            randod:MakeJoints()
            end
        end
        timer() -- don't even know what this is...
        msg:Destroy() -- this too.
    end -- forgot an end...
    end

All these errors are simple debugging mistakes, and the output would explain it better then me.

Log in to vote
0
Answered by
Lacryma 548 Moderation Voter
8 years ago

Alright. This has a lot of problems but I put something together for you.

The problems are as listed:

  1. randround is useless since it will always generate 1.
  2. You cannot loop through a model to get a random model, instead using GetChildren we can allow the children to be randomly selected using this format. models[math.random(1, #models)]
  3. The while loop runs every second. Do not blame me if you run into problems with that.

Solution:

local ODers = game.Lightning.ODers:GetChildren()

while true do
    wait(1)
    if game.ReplicatedStorage.NumberOfModels.Value < 220 then
            for f = 1,15 do
                wait(.5)
                randod = ODers[math.random(1, #ODers)]
                randod.Parent = workspace
                randod:MoveTo(workspace:WaitForChild("EnabledSpawnBrick").Position + Vector3.new(0,10,0)) 
            end
    end
    timer()
    msg:Destroy()
end

Answer this question