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!
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.
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