How can i randomize a loot table?

Note: I’m not a scripter, i do have a very, very basic understading of it.

So, i have been using the Marine AI By kangerujack.

And “I” Modified it so they clone something from “Lighting” upon death.
How can I make it so a random thing gets choosen from a folder within Lighting, upon their death?

The Script (Totally not stolen)
image

1 Like

this is not working code its just to help point you in the right direction

-- get a reference to the folder
local folder = game.ServerStorage.Items

-- get everything inside the folder and save it in a table called children
local children = folder:GetChildren()

humanoid.Died:Connect(function()
    -- clone a random instance inside the folder
    local clone = children[math.random(#children)]:Clone()

    -- position the clone
    clone.Position = humanoid.RootPart.Position

    -- put the clone into the workspace
    clone.Parent = game.Workspace
end)
1 Like

Aight, it works!
Now, how can i set chances for the loot table?

Assign intervals to specific items or categories
Generate a number between 1-100 for instance (so it’s easy to work in %)

As an example, the Rare items have the numbers 50-80 assigned to them, which translates to a 30% drop chance.
If we generate number 67, then we know to select an item from the Rare category.

local items = {
    game.ServerStorage.Item1,
    game.ServerStorage.Item2,
    game.ServerStorage.Item3,
    game.ServerStorage.Item4,
}

local chances = {
    1000,
    100,
    10,
    1,
}

local total = 0

for i, chance in ipairs(chances) do
    total += chance
end

local function SelectRandom()
    local random = math.random(total)
    for i, chance in ipairs(chances) do
        random -= chance
        if random > 0 then continue end
        return items[i]
    end
end

print(SelectRandom())
print(SelectRandom())
print(SelectRandom())
print(SelectRandom())
print(SelectRandom())
1 Like