How to make a math random that dont repeat any time

So i want to make a math random that dosent repeat any time, i want that a folder have the max space of 4 items but i want that the tools is fill up the folder randomly. i have 4 items and i what i said is like when the script run he chose random numer of 1 to 4 and put in the folder and make sure that dont put the same number. So when the items can go randomly putted in the folder, like: First to go to folder is 3 after 1 after 4 after 2. This was just a example of how the script need to work

I know that my script will not work because i even finish but its working.

I aldery search for some codes reviews but i dont find nothing that truely help

			math.randomseed(time() * tick())
			local choosen_tool = tools[math.random(1, #tools)]
			local atrib = choosen_tool:GetAttribute("class")
			print(atrib)
			for i=1, #pc do
				if pc[i]:GetAttribute("class") == atrib then
					math.randomseed(time() * tick())
					local choosen_tool = tools[math.random(1, #tools)]
				end
			end

Btw all tools that is in variable tool have the number atribbute called class

ones of the issues that is pretty strange is the one that. everytime i test it chose number 4.

3 Likes

I saw another Developer Forum Post that was having a similar problem, and I think my solution might apply to your code as well.

1 Like

i tried it now but i dont think worked

			math.randomseed(time() * tick())
			
			local rand = Random.new(100)
			
			for i = 1, #tools, 1 do
				local choosen_tool = rand:NextInteger(1, #pc:GetChildren())
			end
						
			local clone = choosen_tool:Clone()

its having problem at choosen_tool for variable clone bec yes

choosen_tool turns into a number, which cannot be cloned.

Also, choosen_tool is a local variable in the for loop, which means that it would be nil if the loop ended.

ok so i make local choosen outside the loop

Yes. And, replace this code:

choosen_tool = rand:NextInteger(1, #pc:GetChildren())

With this:

choosen_tool = tools[rand:NextInteger(1, #pc:GetChildren())]

So choosen_tool will be an item inside of the tools table.

			local pc = workspace.Carnes.Picanhas:GetChildren()	
			math.randomseed(time() * tick())
			
			local choosen_tool
			local rand = Random.new(100)
			print("Step1")
			
			for i = 1, #tools, 1 do
				choosen_tool = tools[rand:NextInteger(1, #pc)]
			end
			
			print("Step2")
						
			local clone = choosen_tool:Clone()

so i tried the script but nothing work so i put the prints and i see that wont go to step 2, btw i remove #pc:GetChildren() because the variable aldery is a get children

1 Like

Do you see any errors in the output? Also, does workspace.Carnes.Picanhas have any objects inside of it? If it doesn’t, Random:NextInteger() will fail.

1 Like

No workspace.Carnes.Picanhas when the script is not run dont have nothing in it. im trying to make that if have somenthing in it the mathrandom dont get the same item that aldery have in folder Picanhas

1 Like

Simply remove the item selected inside of the tools table.

local chosen = rand:NextInteger(1, #tools)
choosen_tool = tools[chosen]
table.remove(tools, chosen)

Therefore, when randomly choosing an item from the tools table, it won’t choose the same items because they were removed already.

Just make sure to add the tools back inside of the table if you have to do something else with it.

1 Like

yeah i think about that but i cant do that because the script can run multiply times if i touch the part and but now i have a idea :smiley: maybe when the script finish and the item work is aldery finish i can just put the item back to the tools.

1 Like

Hey i forgot to ask but

			local choosen_tool
			
			local rand = Random.new(100)
			local chosen = rand:NextInteger(1, #tools)
			choosen_tool = tools[chosen]
			
			table.remove(tools, chosen)
			
			print("Step2")
						
			local clone = choosen_tool:Clone()

isint the variable clone will not get a clone bec like the tools got removed

1 Like

If all the items in the tools list are not a table, then it won’t be removed.

When getting an item in a table, it makes a clone of that item.

If you get a table in a table, it makes a reference of that item.

So if that table got deleted, the variable that was referencing that table is nil.

1 Like
local folder = game.ServerStorage:WaitForChild("Carninhas"):WaitForChild("Picainhas")
			local tools = folder:GetChildren()
			
			local pc = workspace.Carnes.Picanhas:GetChildren()
			
			math.randomseed(time() * tick())
			local choosen_tool
			
			local rand = Random.new(100)
			local chosen = rand:NextInteger(1, #tools)
			choosen_tool = tools[chosen]
			
			table.remove(tools, chosen)
			
			print("Step2")
						
			local clone = choosen_tool:Clone()

its a folder

1 Like

It’s completely fine then. Instance:GetChildren() makes a separate table.

1 Like

Ok soo i think its was supose to work but now when i run the script the script just get the number 4, i run 4 times and always was number 4 idk what happend i think can bet random variable problem

Maybe try setting the number 100 to something else in Random.new()?

i put the most random thing but i think whe using the wrong function. bec now i run 4 times the same items but now the item number 2 was another item

Acho que seria uma melhor ideia se você colocasse um task.wait() dentro do for loop. Isso vai dar tempo para que o gerador de número pseudo-aleatório possa calcular um resultado diferente.

Mas o script não está sendo executado em uma função For, o script é executado quando o player toca um bloco