Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Why is this giving everyone a weapon?

So im trying to give one person a weapon, but its giving everyone a weapon when i start a test server in studio

local choose = game.Players:GetPlayers()[math.random(1,#game.Players:GetPlayers())]

if choose:FindFirstChild("Backpack") then
game.ReplicatedStorage.Luger:Clone().Parent = choose.Backpack;

end

EDIT

thats not all of it though,

local players = game.Players:GetPlayers()
local using = false

while math.huge do
	wait(5)
using = false
if using == false then
  using = true	
	if (game.Players.NumPlayers <= 1) then -- This will be the less complicated 'invite more' part
		using = true
		game.StarterGui.gui.LocalScript.Event:Fire()
	else
	if(game.Players.NumPlayers >= 2) then 
		wait(5)
		
     target = CFrame.new(0, 50, 0) --could be near a brick or in a new area
for i, player in ipairs(game.Players:GetChildren()) do
   --Make sure the character exists and its torso exists
   if player.Character and player.Character:FindFirstChild("Torso") then
       --add an offset of 5 for each character
      player.Character.Torso.CFrame = target + Vector3.new(0, i * 5, 0)

wait()

local choose = game.Players:GetPlayers()[math.random(1,#game.Players:GetPlayers())]

if choose:FindFirstChild("Backpack") then
game.ReplicatedStorage.Luger:Clone().Parent = choose.Backpack;

end
	end
	end
	end
end 
 end
end

EDIT 2

It seems to be giving out a weapon per player, and sometimes gives someones weapon to another person

1 answer

✓ Accepted answer

This on its own shouldn't be giving every Player a weapon, although it can be optimized slightly:

local choose = game.Players:GetPlayers()[math.random(game.Players.NumPlayers)]

if choose:FindFirstChild("Backpack") then
	game.ReplicatedStorage.Luger:Clone().Parent = chose.Backpack;
end

First, let's fix your formatting and take a better look at your code:

local players = game.Players:GetPlayers()
local using = false

while true do --No need to be fancy.
	wait(5)
	using = false
	if using == false then
		using = true	
		if (game.Players.NumPlayers <= 1) then -- This will be the less complicated 'invite more' part
			using = true
			game.StarterGui.gui.LocalScript.Event:Fire()
		else --The `if` here covered the *entire* other case, so an `else` works just fine on its onw.
			--For future reference, `elseif` is a valid branch. 
			wait(5)
					
			target = CFrame.new(0, 50, 0) --could be near a brick or in a new area
			for i, player in ipairs(game.Players:GetChildren()) do
				--Make sure the character exists and its torso exists
				if player.Character and player.Character:FindFirstChild("Torso") then
					--add an offset of 5 for each character
					player.Character.Torso.CFrame = target + Vector3.new(0, i * 5, 0)
			
					wait()
				end
			end

			--This looks like the problem!
			--Since you put this code inside of the `for` loop above, a random player will get the Luger for *every player in the game!*
			--I moved some ends further up so that this is outside of the loop.
			local choose = game.Players:GetPlayers()[math.random(game.Players.NumPlayers)]
	
			if choose:FindFirstChild("Backpack") then
				game.ReplicatedStorage.Luger:Clone().Parent = choose.Backpack;
			end
		end 
	end
end

3 comments

0

no, ill give it all

0

(im still working on it so it doesnt delete the weapons yet)

0

Thanks! Im new to looping a lot at things at once

This discussion is preserved as it was. New answers, comments, and votes are closed.