Edit: i added the full script
i've been trying to get this script to select a random child with a specific name like "9mm"
rEvent4.OnServerEvent:Connect(function(plr) if ammo.Value < 15 then local backpack = plr.Backpack:GetChildren() local all9mm = {} for i, v in ipairs(backpack) do if v.Name == "9mm" then table.insert(all9mm, v) end end local chosenitem = all9mm[math.random(1, #all9mm)] print(chosenitem.Name) end end)
If I understand what you're trying to do correctly, you could add all children with the name "9mm" to a separate table, then choose a random value from that table
local backpack = plr.Backpack:GetChildren() local all9mm = {} for i,v in ipairs(backpack) do if v.Name == "9mm" then table.insert(all9mm, v) end end local chosenitem = all9mm[math.random(1, #all9mm)] print(chosenitem)