Broken randomizer weapon script

My friend wrote a gear randomizer script but it doesn’t work, I’m not a scripter so I cant help him either

He has looked

Here’s the code:

local RS = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
local Folder = RS:WaitForChild("Folder")

local function onCharacterAdded(character)
    local Tools = Folder:GetChildren()
    local ToolsTable = Tools
    local chosenTools = {}

    repeat
        local selectedIndex = math.random(1, #ToolsTable)
        table.insert(chosenTools, ToolsTable[selectedIndex])
        table.remove(ToolsTable, selectedIndex)
    until #chosenTools >= 1

    local plr = Players:GetPlayerFromCharacter(character)
    for i,v in pairs(chosenTools) do
        Folder:FindFirstChild(v.Name):Clone().Parent = plr.Backpack
    end
end

local function onAdded(player)
    player.CharacterAdded:Connect(onCharacterAdded) 
end

Players.PlayerAdded:Connect(onAdded)

Any help is appreciated, thanks for reading

Is there any errors?

I’m also somewhat confused of the until chosen line when you’re using the >= operator, do you want to get just 1 tool or more than one?

Three tools, one melee, one ranged and one healing

That’ll only return 1 as you never ask it to return 3 of those type, unless you mean there’s 3 weapons and it returns one at random. If you’re only returning one however what’s the need for the table?

Hi,
You have the same problem that i had!
So i think i can help you.

local serverStorage = game:GetService("ServerStorage") 
local Players = game:GetService("Players")
local Folder = serverStorage:WaitForChild("Folder"):GetChildren()

Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		local randomGear = Folder[math.random(1,#Folder)]
		randomGear.Parent = player.Backpack
		randomGear.Parent = character --this will make tool equip automatically
	end)
end)

gear.rbxl (32.1 KB)

If you want multiple tools you can add more folders.

1 Like