Problem with making a clone of a tool and putting it into a players backpack

Hey guys,
I was trying to make a script that clones a tool, puts it in the player’s backpack, and then force equips the tool. I tried this, but it didn’t work. In the output, it says, Attempt to index nil with ‘Backpack’. The script is below so if anyone can help me, please do!

local roundMsg = game.ReplicatedStorage.Values:WaitForChild("roundMsg")
local zombieCount = game.ReplicatedStorage.Values:WaitForChild("zombiesRemaining")
local zombiesAlive = game.ReplicatedStorage.Values:WaitForChild("zombiesAlive")
local gameInProgress = game.ReplicatedStorage.Values:WaitForChild("gameInProgress")
local mapName = game.ReplicatedStorage.Values:WaitForChild("mapName")
local wave = game.ReplicatedStorage.Values:WaitForChild("Wave")
local collectionservice = game:GetService("CollectionService")
local spawners = game.Workspace.Spawns
local plr = game:GetService("Players").LocalPlayer
local Players = game:GetService("Players")
local Maps = game:GetService("ReplicatedStorage"):WaitForChild('Maps'):GetChildren()
local guns = game:GetService("ReplicatedStorage"):WaitForChild("Guns")

while true do
	for i = 15,0,-1 do
		roundMsg.Value = "Game Starting in: "..i
		if i == 1 then
		local ChosenMap = Maps[math.random(1, #Maps)]:Clone()
			print(ChosenMap.Name)
			mapName.Value = ChosenMap.Name
		local Spawns = ChosenMap:FindFirstChild('Spawns'):GetChildren()
		local RandomSpawn = Spawns[math.random(1, #Spawns)]
			ChosenMap.Parent = game.Workspace.Map
			if mapName.Value == "test" then
				local starter =	guns.X16:Clone()
				starter.Parent = plr.Backpack
				wait(0.1)
				plr.Character.Humanoid:EquipTool(starter)
				--spawners.Spawner.Position = Vector3.new(508.081, 460.137, -54.68)
				print("worked1")
			elseif mapName.Value == "test2" then
				local starter =	guns.M14:Clone()
				starter.Parent = plr.Backpack
				wait(0.1)
				plr.Character.Humanoid:EquipTool(starter)
				print("WORKED2")
			elseif mapName.Value == "test3" then
				local starter =	guns.Kar98k:Clone()
				starter.Parent = plr.Backpack
				wait(0.1)
				plr.Character.Humanoid:EquipTool(starter)
				print("WORKEDDDD3")
			end
			roundMsg.Value = "Preparing Map..."
			task.wait(5)
			roundMsg.Value = "Map Chosen: "..ChosenMap.Name
			task.wait(5)
			for _, Player in pairs(Players:GetChildren())do
					collectionservice:AddTag(Player.Character, "Alive")
					Player.Character.HumanoidRootPart.CFrame = RandomSpawn.CFrame
				end
			
			roundMsg.Value = "Game In Progress"
			zombieCount.Value = 3
			zombiesAlive.Value = 3
			wave.Value = 1
			gameInProgress.Value = true

			repeat
				if #collectionservice:GetTagged("Alive") > 0 then
					if zombiesAlive.Value == 0 then
						wait(10)
						--plr.leaderstats.RoundsSurvived.Value = plr.leaderstats.RoundsSurvived.Value + 1
						wave.Value = wave.Value + 1
						
						zombieCount.Value = 3 * wave.Value
						zombiesAlive.Value = 3 * wave.Value
					end
				elseif #collectionservice:GetTagged("Alive") == 0 then
					gameInProgress = false
				end
				task.wait(1)
			until gameInProgress == false
			workspace.Map:ClearAllChildren()
		end
		task.wait(1)
	end
end



I’m sorry that the script is long!

1 Like

Is this a local or a server side script?

2 Likes

This is a server script, but I have another server script that works when I clone a tool and put it into the backpack, here is the other server script that works -

local ReplicatedStorage = game.ReplicatedStorage.Guns
local toolPart = script.Parent
local toolFromStorage = ReplicatedStorage:FindFirstChild(toolPart.Name)
local player = game.Players.LocalPlayer
local cost = 1


if toolFromStorage then
	toolPart.Base.ProximityPrompt.Triggered:Connect(function(player)
		if player.leaderstats.Gold.Value >= cost then
			player.leaderstats.Gold.Value = player.leaderstats.Gold.Value - cost
			local toolCopy = toolFromStorage:Clone()
			toolCopy.Parent = player.Backpack
			wait(0.1)
			player.Character.Humanoid:EquipTool(toolCopy)
			toolPart.Base.ProximityPrompt.Enabled = false
			for count = 0,10,1 do
				script.Parent.Highlight.OutlineTransparency = script.Parent.Highlight.OutlineTransparency + 0.1
				task.wait(0.1)
			end
			script.Enabled = false
		elseif player.leaderstats.Gold.Value < cost then
			print("not enought money")
		end
	end)
end
1 Like

Its giving error becuase of this line:

local plr = game.Players.LocalPlayer

And ur on a server side script calling the local player which is nil

1 Like

But in the other server script above, it works, so what should I do?

Are u sure its really a server script? Or is it a module script

1 Like

Well wheres the script placed maybe you could use that or can you get the player name?

1 Like

It’s definetely a server script, heres a pic,

this script is in a model in workspace
ak4

1 Like

Ok i have no idea how that works but it works i guess where is the other script placed in ur game?

1 Like

There is another script that may connect to the script that works, the script is in starter character scripts and it’s a local script so maybe thats why it works. Anyway thanks for helping

Instead of getting the player by local player you could try putting this into your script:

local plr
local function GetPlayerByName(PlayerName)
for _, player in game:GetService("Players") do 
if player.Name == PlayerName then
return player
end
end
plr = GetPlayerByName()--here u put the respective player name

but uh do you want to equip the tool to only one player or to all players?

1 Like

ok thanks, I’ll try this later