jeudi 29 juin 2023

(Roblox Studio/Lua) How do I place 2 random gears from game.Workspace.items into player.Backpack everytime they spawn or respawn?

I need a script in Roblox Studio (Lua code) that places two random gears from the folder game.Workspace.items into the player's backpack ( player.Backpack I think)

I tried this code and it did nothing:

`
    local player = game.Players.LocalPlayer
    local itemsFolder = game.Workspace.items
    local backpack = player.Backpack
    
local function getRandomItems()
        local items = itemsFolder:GetChildren()
    local selectedItems = {}

    if #items >= 2 then
        local randomIndices = {}

        while #randomIndices < 2 do
            local randomIndex = math.random(1, #items)

            if not table.find(randomIndices, randomIndex) then 
                table.insert(randomIndices, randomIndex)
            end
        end

        for _, index in ipairs(randomIndices) do
            table.insert(selectedItems, items[index])
        end
    end

    return selectedItems
end

 player.CharacterAdded:Connect(function(character)
    backpack:ClearAllChildren()

    local selectedItems = getRandomItems()

    for _, item in ipairs(selectedItems) do
        local clone = item:Clone()
        clone.Parent = backpack
    end
end)`



Aucun commentaire:

Enregistrer un commentaire