Math.Random Problems

Hello, So I am making a cart system…and I want it when I click on an object to add it to cart I want it to select from 4 parts which part it should put in the cart. The others should remain transparent except the one it chose, How do I do that?
Here is a small part of the script:

local function Item()
	local anitem = Items:GetChildren()[math.random(1, #Items:GetChildren())]
	wait(1)
	anitem.Transparency = 0
end
3 Likes
local function Item()
	local anitem = Items:GetChildren()[math.random(1, #Items:GetChildren())]
	task.wait(1)
   for index,value in pairs(Items:GetChildren()) do
      if value ~= anitem then
          Value.Transparency = 1
      end
   end
	anitem.Transparency = 0
end

Hope I understood you correctly

3 Likes

Sadly it doesn’t work…Thank you for your help tho =)

1 Like
local function Item()
	local anitem = Items:GetChildren()[math.random(1, #Items:GetChildren())]
	task.wait(1)
   for index,value in pairs(Items:GetChildren()) do
      if value.Name ~= anitem.Name then -- or compare the objects
          value.Transparency = 1
      end
   end
	anitem.Transparency = 0
end

my bad, I accidentally put ‘Value’ instead of ‘value’

1 Like

Yeah I noticed that mistake and changed it but eh yeah still doesn’t work…

2 Likes

What exactly is it that isn’t working? Are you sure all values are correct?

I changed the script to compare instances rather than names of them, as comparing names can lead to errors since multiple instances may have the same name.

local function Item()
	local anItem = Items:GetChildren()[math.random(1, #Items:GetChildren())]
	task.wait(1)
	for _, child in pairs(Items:GetChildren()) do
		if child ~= anItem then -- or compare the objects
			child.Transparency = 1
		end
	end
	anitem.Transparency = 0
end
1 Like

Still does not work…It just won’t let the part appear and it shows no errors

1 Like

What do you mean appear? Is the transparency of everything 1?

1 Like

Yeah, I want it when a player clicks on a part it will make 1 part appear out of 4 parts

1 Like

Do these prints print?

local function Item()
	local anItem = Items:GetChildren()[math.random(1, #Items:GetChildren())]
	print("Selected: ", anItem)
	task.wait(1)
	for _, child in pairs(Items:GetChildren()) do
		if child ~= anItem then -- or compare the objects
			child.Transparency = 1
			print(child, "hidden.")
		end
	end
	print("Showing", anItem)
	anItem.Transparency = 0
end
1 Like

It prints Selected the item and it also prints showing the item but it’s not making it visible

1 Like
print(child, "hidden.")

If this line isn’t printing then the ‘Items’ instance must contain no children.

1 Like

Yeah it isn’t printing, Tho it has 4 parts inside of it…How?

1 Like

Maybe the parts aren’t anchored, so they fall out of the map and get destroyed? Make sure they’re anchored.

1 Like

They are welded to the handle so yeah I don’t think that’s a problem

1 Like

Paste this code, added additional prints.

local function Item()
	local anItem = Items:GetChildren()[math.random(1, #Items:GetChildren())]
	print("Selected: ", anItem)
	print("Out of:", Items:GetChildren())
	task.wait(1)
	for _, child in pairs(Items:GetChildren()) do
		print("Evaluating", child)
		if child ~= anItem then -- or compare the objects
			child.Transparency = 1
			print(child, "hidden.")
		end
	end
	print("Showing", anItem)
	anItem.Transparency = 0
end

Alright so…It printed:
-Selected Item1
-Out of table…
-Evaluating Item3
-Evaluating Item2
-Evaluating Item1
-Evaluating Item4
-Item4 Hidden
-Showing item 1

local Items = workspace.Items:GetChildren()
local ChosenItem = nil


local function Item()
	local random = math.random(1, #Items)
	local anitem = Items[random]
	ChosenItem = anitem
end

workspace.Part.ClickDetector.MouseClick:Connect(function()
	if ChosenItem ~= nil then
		ChosenItem.Transparency = 0
	end
end)

Try this

If I am not wrong, You forgot to call out the function Item, Or will it not be used?

Oh i guess I did, Try that but call the function item