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

Question about maths?

function thing(model,timeToWait)
	for i,v in pairs(model:GetChildren()) do --important line here.
          for q=1,100 do
      		wait(timeToWait)--maths are needed here
        	end
          end
	end
  print("it should have taken"..timeToWait.."seconds to print this")
end

thing(game.Workspace.Model,10) --6 children in this one.
thing(game.Workspace.Model2,8)

Pretty much it. The first thing() should print "it should have taken10seconds to print this", and the second one should print "it should have taken8seconds to print this"

I'm not very good at math, and I couldn't get this to work right.

1 answer

The idea here is to get how many times the loop will iterate. For every child in the model, you iterate 100 times. So, if you have 4 children, you iterate 400 times. You'd then divide the time wait()ed by 400. If you wanted to wait 8 seconds, that would be 8/400. That's wait(8/400), repeated 400 times. 400 multiplied by 8/400 is 8. So, you've waited 8 seconds in total.

function thing(model,timeToWait)
	local t1 = tick()
	local children = #model:GetChildren() --The amount of children
	for _ = 1, children do
		for _= 1,100 do
			wait(timeToWait/(100 * children))
		end
    end
    local t2 = tick()
  print("it should have taken "..timeToWait.." seconds to print this")
    print("it took "..math.floor(t2 - t1).." seconds to print this")
end

thing(game.Workspace.Model,10)
thing(game.Workspace.Model2,8)

And there's a bit of a problem with this. You can only wait() for a 30th of a second (1/30) or more. If you try to wait for less than that, the wait() function will wait for that 30th of a second instead.

3 comments

0

i had a model with ONE child, and this happend: it should have taken 10 seconds to print this it took 11 seconds to print this

0

Try making the loop run like 50 times or so instead of 100

0

Note that the smallest wait() possible is roughly 0.033 seconds, which is about 1/30 seconds. wait()ing with a delay smaller than this would make it act as if it waited 1/30 seconds.

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