How would I give a random weapon to each player

I am trying to clone a random weapon in my folder and parent it to a random player.
That means each player will have different weapons.

Here is my script

				local ToolsFolder = ServerStorage:WaitForChild("Tools"):GetChildren()
				local RandomWeapon = ToolsFolder[math.random(1, #ToolsFolder)]:Clone()
				
				local IngamePlayers = game.Players:GetPlayers()
				
				RandomWeapon.Parent = IngamePlayers[math.random(1, #IngamePlayers)]

I discovered that it would give a random weapon only to a random player in the game and also the script was not working.

Can anyone help?

3 Likes

I do not know what the RandomWeapon is. Instead of using RandomWeapon use a math.random for the weapons

Put the weapons contained inside the tool folder like this.

 , #Weapons)

I think that’d work for this. In the meantime, just check inside the folder and see the amount of weapons inside that folder, the math.random needs to be contained with the amount of weapons in the folder.

2 Likes

Thanks but how would I give each player a different weapon

1 Like
for _,v in ipairs(Players) do
local RandomWeapon = ToolsFolder[math.random(1, #ToolsFolder)]:Clone()
RandomWeapon.Parent = v.Backpack;
end
2 Likes

the math.random would give the player a random weapon. It would have to give at least everyone a random weapon. You can learn more about math.random here: math | Documentation - Roblox Creator Hub

2 Likes

It looks like you’re trying to parent it to the actual player object. If this is not what you’re trying to do, try these other options:

parent to players backpack

RandomWeapon.Parent = IngamePlayers[math.random(1, #IngamePlayers)].Backpack

parent to player’s character (also forcing them to equip it)

RandomWeapon.Parent = IngamePlayers[math.random(1, #IngamePlayers)].Character
2 Likes

That was the solution to my error. :smiley:
but could you help me one more time?

I want each player to have a random weapon. How is it possible?

The code that @VineyardVine provided already gives each player a random weapon.

1 Like

@EmeraldLimes is it nessecary to use that semi - colon?

No, it is not. You can read more about what a semicolon does here.

1 Like

Make a table of all the weapons. Each time you give a player a weapon, remove that exact weapon from the table and then continue it on and on to all the players.

1 Like

Real thanks it is working but it keeps on cloning tools to the player.
How can I stop this?

Should I do -

				for _,v in ipairs(Players) do
					local RandomWeapon = ToolsFolder[math.random(1, #ToolsFolder)]:Clone()
					RandomWeapon.Parent = v.Backpack;
					wait(1)
					RandomWeapon:Destroy()
				end

Could you take a look outside of the for loop to see if you are running it more than once? The original script shouldn’t be repeating itself.

1 Like

I have figured it out my self.

Thanks for the help you gave me.

1 Like