Armor Module Script Attempt To Index Nil With Clone

  1. I want to make a module script that can easily remove / equip custom armor models for a RPG game.

  2. I have tried putting the armor model in the in the parameters of the function in a server script however in the module script when I try to clone the armor model it returns attempt to index nil with 'Clone' and nothing works.

My Server Script

local Module = require(game.ReplicatedStorage.ArmorModule) -- Require Module
-- Replicated Storage
local rs = game.ReplicatedStorage or game:GetService("ReplicatedStorage")

game.Players.PlayerAdded:Connect(function(plr) -- When Player Joins
	local char = plr.Character or plr.CharacterAdded:Wait()
	-- The Armor Is In rs.ARMORS
	Module.MakeArmor(char, rs.ARMORS:FindFirstChild('Steel'))
	print('Did A Thing') -- This Doesn't Print
end)

My Module Script

local ArmorModule = {} -- Module Name
--Remove old armour.

local rs = game.ReplicatedStorage or game:GetService("ReplicatedStorage")
function ArmorModule.RemoveArmor(character)
	-- Search Through Character For The Names Of Armors
	for i,v in pairs(character:GetChildren()) do
		if v:IsA("Model") then -- Makes Sure That Only Armor Models Are Deleted
			if character:FindFirstChild("LeftUpperArm") then
				character:FindFirstChild("LeftUpperArm"):Remove()
			elseif character:FindFirstChild("RightUpperArm") then
				character:FindFirstChild("RightUpperArm"):Remove()
			elseif character:FindFirstChild("UpperTorso") then
				character:FindFirstChild("UpperTorso"):Remove()
			elseif character:FindFirstChild("LeftUpperLeg") then
				character:FindFirstChild("LeftUpperLeg"):Remove()
			elseif character:FindFirstChild("RightUpperLeg") then
				character:FindFirstChild("RightUpperLeg"):Remove()
			end	
		end
	end
end

local removearmor = ArmorModule.RemoveArmor

function ArmorModule:MakeArmor(character, armor)
	removearmor(character) -- Removes the already equipped armor
	print(armor) -- Why Does This Print Nil?
	--local armorpart = rs.ARMORS:FindFirstChild('Steel') -- This doesn't work Either
	local armorpart = armor:Clone() -- Why Does This Say "attempt to index nil with 'Clone'"?
	-- Search Through The Cloned Armor
	for a, b in pairs(armorpart:GetChildren()) do
		print(b.Name) -- This Doesn't Print
		-- Search Through The Armor Models In The Cloned Armor
		for i,v in pairs(b:GetChildren()) do
			print(v.Name) -- This Doesn't Print
			v.Anchored = false
			v.CanCollide = false
			if v.Name == 'Middle' then 
				v.Transparency = 1
				-- Weldy Things
				local Y = Instance.new("Weld")
				Y.Part0 = character[b.Name] -- This says Cannot Weld A Model when there are no models in the character
				Y.C0 = CFrame.new(0, 0, 0)
				Y.Parent = Y.Part0
			else
				-- More Weldy Things
				local W = Instance.new("Weld")
				W.Part0 = b.Middle
				W.Part1 = v
				local CJ = CFrame.new(b.Middle.Position)
				local C0 = b.Middle.CFrame:inverse()*CJ
				local C1 = v.CFrame:inverse()*CJ
				W.C0 = C0
				W.C1 = C1
				W.Parent = b.Middle
			end
		end
		--Parents The Finished Armor Model
		b.Parent = character
	end
end

return ArmorModule

If You Need The Armor Folder With Armor Models

If I made any mistakes in the code or if its not optimized then do tell me. Any example scripts would be greatly appreciated and thanks for the help in advance. :happy1:

1 Like

This is essentially the same issue as here: Player object not being passed into Module Script - #2 by Autterfly

You’re correctly calling your code with ., but your function is incorrectly defined using :. Since you have it defined using :, an invisible “self” parameter is present. This makes all your arguments be off by one in placement, so armor is actually stored in character, and character in self.

You can solve this by changing function ArmorModule:MakeArmor to function ArmorModule.MakeArmor, which drops the implicit parameter.

make sure your Armors folder actually exists within ReplicatedStorage. (and its probably best to keep it in ServerStorage, since the client shouldn’t access the armors folders)

you can use this to wait for the childs existence.

:WaitForChild("Armors"):WaitForChild("Steel")

EDIT: actually autterfly is right

change your removeArmor() function format to this

ArmorModule.RemoveArmor = function(character)

end

OR THIS (preferred)

function ArmorModule:RemoveArmor(character)

end