Random.new printing the same number

I read off this table thing of things and I print out the second index which is the Randow.new function but it only gives me the same number every time I print it

local Items = {
	
	["Possible Items"] = {
		
		
		["Block"] = {"Block",random:NextInteger(0,50)};
		["Sphere"]  = {"Sphere", random:NextInteger(0,50)};
		
		}
	
	
}

It looks like you’re just calling NextInteger once for each item and storing the result, so each time you read from the table you’re just reading the same stored value.

I have random.new at the top of the script though

local random = Random.new(tick())
local Items = {
	
	["Possible Items"] = {
		
		
		["Block"] = {"Block",random:NextInteger(0,50)};
		["Sphere"]  = {"Sphere", random:NextInteger(0,50)};
		
		}
	
	
}

What I mean is you are only generating one random number for each of the table values. Each time you read the table it will not give you a new random number, it’s just giving you the number stored in the table.

I don’t exactly know the context of your script, but if this table is being duplicated then you would have to call NextInteger every time the table is copied.

I have a local script that is requiring the module that I have in above

tbl[2] is the random function

uis.InputBegan:Connect(function(i,t)
	if t then return end
	if i.KeyCode == Enum.KeyCode.F then
		for idx, tbl in pairs(Item["Possible Items"]) do
			for i,v in pairs(workspace:GetChildren()) do
				if v.Name == tbl[1] then
					Stuff:PickUp(plr,v, tbl[2])
				end
			end
		end
	end
end)

Try this small change:

local random = Random.new(tick())

uis.InputBegan:Connect(function(i,t)
	if t then return end
	if i.KeyCode == Enum.KeyCode.F then
		for idx, tbl in pairs(Item["Possible Items"]) do
			for i,v in pairs(workspace:GetChildren()) do
				if v.Name == tbl[1] then
					Stuff:PickUp(plr,v, random:NextInteger(0, tbl[2]))
				end
			end
		end
	end
end)

And the module would look like this:

local Items = {
	
	["Possible Items"] = {
		
		
		["Block"] = {"Block", 50};
		["Sphere"]  = {"Sphere", 50};
		
		}
	
	
}

Basically you can just have the number range in the main storage, then when picking up an item generate the random amount there based on that number.

1 Like

I was thinking about doing something like this, nonetheless it worked and thank you for taking the time to help me, really thank you so much for your help
image