-
I want to make a module script that can easily remove / equip custom armor models for a RPG game.
-
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.