Giving one player one weapon randomly

Hi I am making a mini game sort of type game, and I basically want to random give players different weapons so for example if the theme is bows then one player would get a blue bow the other would get a green bow etc.

weapons and everything is done, but do not know how to give the players only 1 weapon randomly.

This is my part of script where I define the weapons, all I need to do is to give the randomly chosen weapon to the players.

local WepTheme = game.ServerStorage.Weapons:GetChildren()
local ChosenWepTheme = WepTheme[math.random(1,#WepTheme)]
ChosenWepTheme:GetChildren()
2 Likes

As I replied someone about this topic yesterday, you should use for loop.

1 Like

So I am not really good at for loops. that was the main reason why I asked on devforum

1 Like

Dont do that, you can do
local players = game:GetService(“Players”)

Local RandPlayer = Players[math.random(1,#Players)]

This will give a player

1 Like
local getWeapons = game.ServerStorage.Weapons:GetChildren()

local list = {}

for i, parts in pairs(getWeapons) do		
    table.insert(list, math.random(1, #getWeapons))
end

print(list[1])
1 Like

how do I give them only 1 weapon from the list, also I think you will have to remove that weapon that has been given from the list so multiple players don’t get the same weapon.

So you can do

local weapon = table.find(list, math.random(1, #list))

You can use it in another for loop which gets players and gives random weapons.

for i, player in pairs (game.Players:GetPlayers()) do
    local weapon = table.find(list, math.random(1, #list))

end
1 Like

You can generate a random number and get a value from the list using index list[random number here].

Then to remove the weapon just define list again when the weapon is parented to the player.

1 Like

I did this and got an error
ServerScriptService.MainScript:79: attempt to index nil with ‘Clone’

Could you show me your code on that line?

1 Like

local giveWeapon = weapon:Clone()

When this error occur, that means Clone do not exist. You probably misspelled something.

2 Likes

@Furkan5E Could you show us like the code around the error line please?

1 Like
				local WepTheme = game.ServerStorage.Weapons:GetChildren()
				local ChosenWepTheme = WepTheme[math.random(1,#WepTheme)]
				local weps = ChosenWepTheme:GetChildren()

				for i, player in pairs (game.Players:GetPlayers()) do
					local weapon = table.find(weps, math.random(1, #weps))

					local giveWeapon = weapon:Clone()
					giveWeapon.Parent = player:WaitForChild("Backpack")
				end
1 Like

Are there any actual weapons in the table?

1 Like

Try local weapon = weps[math.random(1,#weps)]

1 Like

I think you were overcomplicating it and it wasn’t needed to use table.find as it can return nil.

I am very confused, how would you do it?

1 Like

I would do local weapon = weps[math.random(1,#weps)]. It’s more compact.