Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 5 years ago
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

Log in to vote
1
Answered by 5 years ago
Edited 5 years ago

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.

0
Is Itemlist[1] the same as just doing game.ServerStorage.Items.Sword? (In this case, object 1 in table is a sword.) Thepoint13 99 — 5y
0
Yeah it would be msuperson24 69 — 5y
Ad

Answer this question