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

[Solved]How do I get this to select a random dropper each time in an efficient way?

Here is my code:

-- module script
local function runDroppers(Baseplate)
	
	local ranNum = Random.new(tick())
	local purchasedItems = Baseplate:FindFirstChild("PurchasedItems")
	local debris = game:GetService("Debris")
	local oreToClone = Instance.new("Part")
	local cash = Instance.new("NumberValue")
	
	if purchasedItems then
		
		local itemsTbl = purchasedItems:GetChildren()
		
		purchasedItems.ChildAdded:Connect(function(child)
			
			table.insert(itemsTbl, child.Name)
			
		end)
	
	
		while Baseplate do
			
			wait(2)
			
			local newTbl = {}
			
			if #itemsTbl > 0 then
			
				for i,v in pairs(itemsTbl) do
					
					wait()

					if v:FindFirstChild("ItemType") then
						if v.ItemType.Value == "Dropper" then
							
							table.insert(newTbl, v.Name)
							
						end
						
					end
					
				end
			
				
				for i = 1, #newTbl do
					
					wait(.3)
					
					local ranIndex = ranNum:NextInteger(1, #newTbl)
					local dropper = purchasedItems:FindFirstChild(newTbl[ranIndex])
	
					if dropper then
						
						local oreValue = dropper:WaitForChild("OreValue")
						local dropperPart = dropper:WaitForChild("DropperPart")
						
						table.remove(newTbl, ranIndex)
						

						if oreValue and dropperPart then
									
							local newOre = oreToClone:Clone()
									
							newOre.Name = "Ore"
							newOre.Size = Vector3.new(1, 1, 1)
							newOre.Position = dropperPart.Position
							newOre.Parent = dropper
									
							local newCash = cash:Clone()
											
							newCash.Name = "Cash"
							newCash.Value = oreValue.Value
									
							debris:AddItem(newOre, 20)
									
							newCash.Parent = newOre					
									
						end
						
					end	
						
				end
				
			end
			
		end
	
	end
	
end

return runDroppers

What this is supposed to do is have the droppers drop the ores in a random order every couple of seconds. The problem is that they drop it in a random order, but it is the same random order ever time after the first loop. I tried rejoining the game and it was a different order that time but it stayed the same order even though it was different. The second question I have is: Is this even the most effective way to go about what I am trying to do? Thanks!

2 answers

This is because when you choose a random number, it uses a specific list of numbers and uses a randomseed to determine where to start on that list. To ensure you get a 100% random order each time, everytime you pick a random number, put math.randomseed(tick()), this will update the list everytime you pick a new number from the RNG.

4 comments

0

"100% random order" is actually not entirely true. Yes, it is unpredictable since you're using a dynamic time value, but the number itself is still calculated under the same rules. Fundamentwlly "random," though.

0

look at line 4

0

I think that you should read up a little on Random.new()

0

thought Random.new() was just a C# thing, huh

Personally, I don't really think Random.new is necessary for this, but I have never actually used it, so it may generate numbers differently than I think it does. I would just use math.random(min,max) to generate random numbers. Like you did, you can put all the droppers in a table and use the random number to index them...

local droppers = {--[[dropers are in here]]}

local index = math.random(1,#droppers)

droppers[index]... -- Do dropping stuff

However with that way obviously you might get occurrences where it may run the same dropper more than twice. To solve that, what you can do is each time a dropper drops something, remove it from the table...

local tempTable = droppers

for i = 1,#droppers do
	local index = math.random(1,#tempTable)
	tempTable[index]... -- Do dropping stuff
	table.remove(tempTable, index)
end

3 comments

0

look at line 57

0

The Random object has a better random algorithm than math.random. That is why I am using it.

0

I use it for all of my new work

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