Script that gives random player a weapon not working

I tried to make a script where it chooses a random person in the server and gives them a weapon. It works with a 1 Player server but not with a server with more than one player. Here’s the script:

local playertable = {}

local repstore = game:GetService(“ReplicatedStorage”)

local weapon = repstore.ClassicSword

local plrs = game.Players

while wait(1) do
local chosenplr = plrs:GetChildren()[math.random(1, #plrs:GetChildren())]

local murderweapon = weapon:Clone()
murderweapon.Parent = chosenplr.Backpack
end

Please help!!!

How doesn’t it work, like explain what’s not working.

1 Like

wdym???..

Your just saying “it doesn’t work” what doesn’t work though.

What is happening that you didn’t expect to happen?

ohhh, i have a weapon in repstore, it wont give the weapon to the player. but it only gives the weapon to the player in a 1 player server.

So basically the problem is, in a two player server the weapon isn’t being to anyone right?

Are you using a Server Script? Local Scripts won’t work. Also where is this script located?

1 Like

sss, is normal script…

yes, and in 3 players server, 4, 5, and so on

also, this pops up
Screenshot_10

Ok, I made a script which is an example of how this is works (It works i tested it) here is the script:

And I figured out what was wrong. You forgot to see if the amount of players was 0 (when the server first starts)

while wait(1) do
	
	local Players = game.Players:GetPlayers()
	
	if #Players ~= 0 then -- before you didn't check if the amount of players is 0
		local RandomPlayer = Players[math.random(1,#Players)]
		
		print(RandomPlayer)
	end
	
end

this pops up
Screenshot_10

Yah, look at the script I gave you above you’ll understand what you did wrong.

You forgot to check if the amount of players was 0.

thank you soooo much!..