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

Simple Math.random error I can't fix?

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()

2 comments

0

There are more than one thing wrong with this. What are you trying to do?

0

Randomly select one child out of a model and clone it.

3 answers

✓ Accepted answer

If you want a random clone from that just do

Model = blah
clone = Model:GetChildren()[math.random(1,#Model:GetChildren())]:Clone()
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.

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

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