Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

Why is this giving everyone a weapon?

Asked by 9 years ago

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

Log in to vote
2
Answered by
adark 5487 Badge of Merit Moderation Voter Community Moderator
9 years ago

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
0
no, ill give it all bubbaman73 143 — 9y
0
(im still working on it so it doesnt delete the weapons yet) bubbaman73 143 — 9y
0
Thanks! Im new to looping a lot at things at once bubbaman73 143 — 9y
Ad

Answer this question