Issue with giving gun to multiple players

  1. What do you want to achieve? - I’m making a low poly FPS game. and I made a gun for it.

  2. What is the issue? - The gun spawns perfectly with 1 player. But when the 2nd player joins, they don’t get the gun.

  3. What solutions have you tried so far? - I’ve tried mixing up the script a bit.

Here’s the script:

local ss = game:GetService("ServerStorage") -- Put your tool in ServerStorage!
local plrs = {}

repeat wait(1) until game.Players.NumPlayers >=1

for i, player in pairs(game.Players:GetPlayers()) do
	if player then
		table.insert(plrs,player)
	end
end

wait(2)

for i, player in pairs(plrs) do
	if player then
		character = player.Character
		local humanoid = player.Character.Humanoid
		
		if character then
			--gives the weapon
			local pistol = ss.Pistol:Clone()
			pistol.Parent = player.Backpack
			character.Humanoid:EquipTool(pistol)
			wait(.5)
			
		else
			--no char/plr left
			if not player then
				table.remove(plrs, i)
			end
		end
	end
end

Any solutions/help?

Your script is kind of wonky here. It assumes the player’s character exists by trying to get the Humanoid but then actually checks for it’s existence right after.

This is what it should look like:

for _, player in pairs(game.Players:GetPlayers()) do
   local character = player.Character -- make this local
   -- the humanoid variable wasn't necessary

   if character then
	  local pistol = ss.Pistol:Clone()
	  pistol.Parent = player.Backpack
	  character.Humanoid:EquipTool(pistol)
   end
end

Also, your script didn’t update the plrs table.

But I assume the player gets a weapon immediately after they join, so why not just parent the Pistol to StarterPack?

The backpack UI is hidden, like Arsenal. There’ll be a UI backpack at some point.

So it should be:

local ss = game:GetService("ServerStorage") -- Put your tool in ServerStorage!
local plrs = {}

repeat wait(1) until game.Players.NumPlayers >=1

for i, player in pairs(game.Players:GetPlayers()) do
	if player then
		table.insert(plrs,player)
	end
end

wait(2)

for _, player in pairs(game.Players:GetPlayers()) do
	local character = player.Character -- make this local
	-- the humanoid variable wasn't necessary

	if character then
		local pistol = ss.Pistol:Clone()
		pistol.Parent = player.Backpack
		character.Humanoid:EquipTool(pistol)
			end
		end
	end
end
1 Like

You can do that or this:

local function bind(player)
   -- function to give the tool to the player
   local character = player.Character or player.CharacterAdded:Wait() 
   -- gets the character or waits for it to load

   local function giveTool(char)
       local pistol = ss.Pistol:Clone()

       if player.Backpack:FindFirstChild(pistol.Name) then
          -- stop the function if the player already has the tool
          -- if the player doesn't get the tool after they respawn anymore, just remove this if condition
          char.Humanoid:EquipTool(players.Backpack[pistol.Name])
          return
       end

       -- add the 
       pistol.Parent = player.Backpack
       char.Humanoid:EquipTool(pistol)
   end

   -- gives the player the pistol when their character spawns
   giveTool(character)
   player.CharacterAdded:Connect(giveTool)
end

-- give the player the tool when they join
game.Players.PlayerAdded:Connect(bind)

-- loop through the players already in the game
for _, plr in pairs(game.Players:GetPlayers()) do
   bind(plr)
end