How to give all players a weapon?

try this one

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


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

1 Like

then fire the function whenever with GiveSword()

Nothing happens there either.

Perhaps I should include the rest of the script that this code is running in.

-- VARIABLES --

local BP = game.Workspace.Baseplate

local OGdensity = 0.7
local OGelasticity = 0.5
local OGelasticityWeight = 1
local OGfriction = 0.3
local OGfrictionWeight = 1

local friction = 0.004
local frictionWeight = 100

local physProperties = PhysicalProperties.new(OGdensity, friction, OGelasticity, frictionWeight, OGelasticityWeight)
local OGphysProperties = PhysicalProperties.new(OGdensity, OGfriction, OGelasticity, OGfrictionWeight, OGelasticityWeight)

local plr = game.Players.LocalPlayer

-- VARIABLES --

while true do
	local chosenNumber = math.random(1, 6)
	if chosenNumber == 1 then
		BP.CustomPhysicalProperties = physProperties
		
		BP.Material = ("Ice") -- TURN BASEPLATE TO ICE
		BP.BrickColor = BrickColor.new("Pastel Blue")
		warn('Iced Baseplate')
	end
	if chosenNumber == 2 then
		BP.CustomPhysicalProperties = OGphysProperties
		
		BP.Material = ("Plastic") -- TURN BASEPLATE BACK TO NORMAL
		BP.BrickColor = BrickColor.new("Dark stone grey")
		warn('Reset BasePlate Ice')
	end
	if chosenNumber == 3 then
		BP.CustomPhysicalProperties = OGphysProperties

		BP.Material = ("Plastic") -- TURN BASEPLATE BACK TO NORMAL (This is here twice to double the change of it happening.)
		BP.BrickColor = BrickColor.new("Dark stone grey")
		warn('Reset BasePlate Ice')
	end
	if chosenNumber == 4 then
		warn('Gave player sword')
		
		local plr = game.Players.LocalPlayer

		game:GetService("Players").PlayerAdded:Connect(function(player)
			game.ServerStorage.ClassicSword:Clone().Parent = plr.Backpack
		end)
	end
	if chosenNumber == 5 then
		warn('5') -- I'm gonna add stuff here later.
	end
	if chosenNumber == 6 then
		warn('6') -- I'm gonna add stuff here later.
	end
	wait(5)
end

Is something in there causing it not to work?

so its a local script?
hold up gonna edit this reply soon

It’s a server script. Not a local script

ok so just use my old function like so:

--FUNCTIONS--
local function GiveSword()
	wait()
	for _, Players in pairs(game.Players:GetChildren()) do
		local characters = workspace[Players.Name]
		if characters:FindFirstChild("ClassicSword") then

		else
			game.ServerStorage.ClassicSword:Clone().Parent = characters
		end

	end
end
-- VARIABLES --
local BP = game.Workspace.Baseplate

local OGdensity = 0.7
local OGelasticity = 0.5
local OGelasticityWeight = 1
local OGfriction = 0.3
local OGfrictionWeight = 1

local friction = 0.004
local frictionWeight = 100

local physProperties = PhysicalProperties.new(OGdensity, friction, OGelasticity, frictionWeight, OGelasticityWeight)
local OGphysProperties = PhysicalProperties.new(OGdensity, OGfriction, OGelasticity, OGfrictionWeight, OGelasticityWeight)

local plr = game.Players.LocalPlayer

-- VARIABLES --

while true do
	local chosenNumber = math.random(1, 6)
	if chosenNumber == 1 then
		BP.CustomPhysicalProperties = physProperties

		BP.Material = ("Ice") -- TURN BASEPLATE TO ICE
		BP.BrickColor = BrickColor.new("Pastel Blue")
		warn('Iced Baseplate')
	end
	if chosenNumber == 2 then
		BP.CustomPhysicalProperties = OGphysProperties

		BP.Material = ("Plastic") -- TURN BASEPLATE BACK TO NORMAL
		BP.BrickColor = BrickColor.new("Dark stone grey")
		warn('Reset BasePlate Ice')
	end
	if chosenNumber == 3 then
		BP.CustomPhysicalProperties = OGphysProperties

		BP.Material = ("Plastic") -- TURN BASEPLATE BACK TO NORMAL (This is here twice to double the change of it happening.)
		BP.BrickColor = BrickColor.new("Dark stone grey")
		warn('Reset BasePlate Ice')
	end
	if chosenNumber == 4 then
		warn('Gave player sword')
		GiveSword()
	end
	if chosenNumber == 5 then
		warn('5') -- I'm gonna add stuff here later.
	end
	if chosenNumber == 6 then
		warn('6') -- I'm gonna add stuff here later.
	end
	wait(5)
end

and on a side note you cant get the LocalPlayer from a server script

1 Like

Thank you so much! It’s works! There’s just one problem…
You can get the sword more than once. Is there a way to keep it from duplicating?

yep change the function into

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

	end
end

Wow, thank you! That helps me out more than you can imagine.

1 Like

Sorry to re-open this. The weapon still duplicates if the player isn’t holding it.

Hello CbrahDev!

I re-wrote the code to be more organized, clean, and optimized. Please mark mine as the solution if it works to your abilities, if not I suggest creating a new topic or unmarking a solution for this one.

local function giveallplayers(tool)
	for i,v in pairs(game.Players:GetPlayers()) do
		if v.Character then
			if v:FindFirstChild("Backpack") then
				if not v.Backpack:FindFirstChild(tool.Name) then
					if not v.Character:FindFirstChild(tool.Name) then
						local newtool = tool:Clone()
						newtool.Parent = v.Backpack
					end
				end
			end
		end
	end
end
giveallplayers(game.ServerStorage.TestTool)
6 Likes