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

How do I get a item in a folder respective to the math.random value?

local REMOTE = Instance.new("RemoteEvent")
REMOTE.Name = "Box_event"
REMOTE.Parent = game.ReplicatedStorage



game.ReplicatedStorage.Box_event.OnServerEvent:Connect(function(Player,Item)
local ItemsList = game.ServerStorage.Items:GetChildren()
	if (Player.Character.HumanoidRootPart.Position - Item.Position).magnitude < 10 then
		local ChosenItem = math.random(1, #ItemsList)
		local Clone = game.ServerStorage.Items[ChosenItem]:Clone() -- problem
		Clone.Parent = Player.Backpack
	end
end)

On the math.random, I want it to choose a number and on the next line, copy that tool that is that number in the folder. But it only returns a number instead.

1 answer

✓ Accepted answer

You're actually very close. Your ItemsList table has an index of all of the items in the folder already, so after using math.random(1, #ItemsList), there is no need to retrieve the folder again, you could just do

local Clone = ItemsList[ChosenItem]:Clone()

or even

local Clone = ItemsList[math.random(1, #ItemsList)]:Clone()

and save a line.

Edit: I should probably also explain why you can pull it from ItemsList and not from the folder itself. The ItemsList that gets the children from the folder is a table that recognizes the position in the table as a numeric value, whereas the folder itself doesn't recognize a position as a child.

2 comments

0

Is Itemlist[1] the same as just doing game.ServerStorage.Items.Sword? (In this case, object 1 in table is a sword.)

0

Yeah it would be

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