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!