How to give all players a weapon?

I’m trying to make a script that gives all players a sword when I want it to. I’ve been stuck on this problem for a while. Can anyone help me?

The script I’m currently using:

local plr = game.Players.LocalPlayer

game:GetService("Players").PlayerAdded:Connect(function(player)
	game.ServerStorage.ClassicSword:Clone().Parent = plr.Backpack

Note: There’s nothing in the output.

1 Like

Put this in a server-script in ServerScriptService:

game:GetService("Players").PlayerAdded:Connect(function(player)
	game.ServerStorage.ClassicSword:Clone().Parent = player.Backpack
end)

It’s already in a server script.

Use this script.

game.Players.PlayerAdded:Connect(function(plr)

game.ServerStorage.ClassicSword:Clone().Parent = plr:WaitForChild("StarterGear")

game.ServerStorage.ClassicSword:Clone().Parent = plr:WaitForChild("Backpack")

end)

Well lets say you only want them to get a weapon when they touch a certain part:

Assuming this is a regular script inside a part:

local sword = game.serverstorage.ClassicSword

script.Parent.Touched:Connect(function(man)
local player = game.Players:FindFirstChild(man)
local dupescword = sword:Clone()
dupesword.Parent = player
end

I have not tried if this works

you could make it a command in game if you want.

something like this:
I haven’t tested it.

game.Players.PlayerAdded:Connect(function(player)

player.Chatted:Connect(function(msg)
if player.Name == "CbrahDev" then
if msg == ":giveswords" then
for _, v in pairs(game.Players:GetDescendants()) do
if v:IsA("Player") then
game.ServerStorage.ClassicSword:Clone().Parent = v.Backpack
end
end
end
end
end)
end)
1 Like

:GetPlayers() would probably be better to use in a situation like that.
But you’re on the right track, if he’s wanting to give them a weapon at a specific time then a loop over the Player’s list is probably your best bet.

Yeah, probably should have used :GetPlayers().

1 Like

Set it up like this…
image
script like this…

and whenever you want to give the players swords use a local script with the following code:

game.ReplicatedStorage:WaitForChild("GiveSwords"):FireServer()

this will allow you to control the when they get the swords

There is not even a point to loop through all the players. All that was needed was to do game.Players:FindFirstChild(playerName)

Yes I know, I just made a quick script and didn’t check over it.

if you do decide to use this make sure to fire the remote event when you want them to get the swords
fire remote event which is the: game.ReplicatedStorage:WaitForChild(“GiveSwords”):FireServer()

I need to fire the event through a server script.

I think you’re misunderstanding the question and the solution, for one the topic is how to give all players a weapon at a specific time not just an individual. Therefor looping over the Player’s list is in-fact ideal; however I don’t think the original post creator is wanting it to be a command; he should just make a simple function

local function GiveWeapon()
   for i,v in pairs(game.Players:GetPlayers()) do
      game.ServerStorage.ClassicSword:Clone().Parent = v.Backpack;
   end
end

and just do GiveWeapon() when he wants to give the players a weapon

1 Like
local function GiveSword()
	wait()
	for _, Players in pairs(game.Players:GetChildren()) do
		local characters = workspace[Players.Name]
		if characters:FindFirstChild("ClassicSword") then
			
		else
			script.ClassicSword:Clone().Parent = characters
		end
		
	end
end


game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(Character)
		wait(5)
		GiveSword()
	end)
end)


as the man above me has stated(very handsome btw) you should just use a function
i remade his function and came up with this. oh and make sure the swords parent is the script or just change the script.ClassicSword to wherever the sword is, and obviously replace where the GiveSword() goes

That script I gave does exactly that. It gives the player the weapon on join and keeps it perm.

No where does he state he wants to give the weapon on spawn. He says “When I want it to”; if he wanted the person to spawn with a sword he wouldn’t even need a script to do so, he could just put it in the starter gear section in studio and it would handle it automatically. I will grant you that his example code uses PlayerAdded tho which makes both the question and the example very misleading so maybe some clarification is needed.

Why not just put the weapon in the StarterPack, so the game will automatically give all players the sword?

because he wants to control when the players get the swords

Sorry if I was unclear. I want to be able to control when the player gets the weapon.

Random example:
When a player enters a room, all players get a sword.

(Hopefully that makes more since)