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

Get a random child with a specific name?

Asked by 3 years ago
Edited 3 years ago

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)

1 answer

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

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)
0
it works thank you! GameBuilderLol 58 — 3y
0
ok wait i have a problem GameBuilderLol 58 — 3y
0
the script prints out 2 or more random items when its just supposed to be 1 random item GameBuilderLol 58 — 3y
0
make sure the last two lines are outside of the for loop so that they only happen one time OfficerBrah 494 — 3y
View all comments (2 more)
0
i did but it still happens GameBuilderLol 58 — 3y
0
ok it worked, just had to put a if not debounce function between them GameBuilderLol 58 — 3y
Ad

Answer this question