How can I make this armor script more efficient?

I made an armor script which will locally give your armor just for show but the problem is that I have 3 divisions; NTF, Alpha-1 and Sigma-9 with their respective ranks about 5 for each division.

I don’t want to create like 20 scripts for each one of the armor but the best try to limit 1 script per division

for i,v in pairs(game.Players.LocalPlayer.Character:GetChildren()) do
	if v:IsA("Accessory") then
		v:Destroy()
	end
end

wait(1)

local Shirt = Instance.new("Shirt")
Shirt.Name = "Shirt"
Shirt.Parent = game.Players.LocalPlayer.Character

local Pants = Instance.new("Pants")
Pants.Name = "Pants"
Pants.Parent = game.Players.LocalPlayer.Character


Shirt.ShirtTemplate = "rbxassetid://1276417973"
Pants.PantsTemplate = "rbxassetid://1276419277"

local Helmet = game.Lighting.MTF["NTF Sergeant"].Helmet:Clone()
Helmet.Parent = game.Players.LocalPlayer.Character
game.Lighting.MTF["NTF Sergeant"].Helmet.Handle.Position = game.Players.LocalPlayer.Character.Head.Position
local Weld = Instance.new("Weld")
Weld.Parent = Helmet
Weld.Part0,Weld.Part1 = game.Players.LocalPlayer.Character.Head, Helmet.Handle
Weld.C0 = CFrame.new(0,0,-0.6) 
Weld.C1 = game.Players.LocalPlayer.Character.Head.FaceFrontAttachment.CFrame

local Torso = game.Lighting.MTF["NTF Sergeant"].Torso:Clone()
Torso.Parent = game.Players.LocalPlayer.Character
game.Lighting.MTF["NTF Sergeant"].Torso.Handle.Position = game.Players.LocalPlayer.Character.Head.Position
local Weld = Instance.new("Weld")
Weld.Parent = Torso
Weld.Part0,Weld.Part1 = game.Players.LocalPlayer.Character.UpperTorso, Torso.Handle
Weld.C0 = CFrame.new(0,-0.18,-0.45) 
Weld.C1 = game.Players.LocalPlayer.Character.UpperTorso.BodyFrontAttachment.CFrame

local LowerTorso = game.Lighting.MTF["NTF Sergeant"].LowerTorso:Clone()
LowerTorso.Parent = game.Players.LocalPlayer.Character
game.Lighting.MTF["NTF Sergeant"].LowerTorso.Handle.Position = game.Players.LocalPlayer.Character.Head.Position
local Weld = Instance.new("Weld")
Weld.Parent = LowerTorso
Weld.Part0,Weld.Part1 = game.Players.LocalPlayer.Character.LowerTorso, LowerTorso.Handle
Weld.C0 = CFrame.new(0,0.15,0) 
Weld.C1 = game.Players.LocalPlayer.Character.LowerTorso.WaistRigAttachment.CFrame

local RightLeg = game.Lighting.MTF["NTF Sergeant"].RightLeg:Clone()
RightLeg.Parent = game.Players.LocalPlayer.Character
game.Lighting.MTF["NTF Sergeant"].RightLeg.Handle.Position = game.Players.LocalPlayer.Character.Head.Position
local Weld = Instance.new("Weld")
Weld.Parent = RightLeg
Weld.Part0,Weld.Part1 = game.Players.LocalPlayer.Character.RightLowerLeg, RightLeg.Handle
Weld.C0 = CFrame.new(0,0.3,0) 
Weld.C1 = game.Players.LocalPlayer.Character.RightLowerLeg.RightKneeRigAttachment.CFrame

local LeftLeg = game.Lighting.MTF["NTF Sergeant"].LeftLeg:Clone()
LeftLeg.Parent = game.Players.LocalPlayer.Character
	game.Lighting.MTF["NTF Sergeant"].LeftLeg.Handle.Position = game.Players.LocalPlayer.Character.Head.Position
local Weld = Instance.new("Weld")
Weld.Parent = LeftLeg
Weld.Part0,Weld.Part1 = game.Players.LocalPlayer.Character.LeftLowerLeg, LeftLeg.Handle
Weld.C0 = CFrame.new(0,0.3,0) 
Weld.C1 = game.Players.LocalPlayer.Character.LeftLowerLeg.LeftKneeRigAttachment.CFrame
  • some of the armors may have like 2 leg pieces and some 1 so im also aiming to making 1 script which wont break if that is missing
2 Likes

How about you make Reusable Functions?

For Example

local function AddArmorPart(ArmorPart,ArmorPartPositiont,WP1,WP2,WC0,C1)
local instance = ArmorPart:Clone()
instance.Parent = game.Players.LocalPlayer.Character
instance.Position = ArmorPartPosition
local Weld = instance.new("Weld")
Weld.Parent = instance
Weld.Part0, Weld.Part 1 = WP1, WP2
Weld.C0 = WC
Weld.C1 = C1
end

And Make a function for Each type of Armor

function Armor1()
AddArmorPart() -- for head
AddArmorPart() -- for Torso
end

function Armor2()
AddArmorPart()-- for head
AddArmorPart() -- for Torso
end

Since you are making it for an SCPF group (guess), you can automatically give Team their own by checking their teams, or accordingly to their groups.

if game.LocalPlayer.TeamColor = "Team1" then
Armor1()
else
Armor2()
end

I just wanted to give you a concept or an Idea, You may have to adjust it according to your requirements.

1 Like

You shouldn’t be using Lighting as a storage. Use something like ServerStorage instead

1 Like

Here’s another approach, pretty similar to MrMouse2405’s solution. But instead of having code that determines which armor parts get added to what armor set, as well as how it’s done, it’s all just data. Data in the two lookup tables and data in the form of what armor pieces are inside each armor set in your armor storage location.

Code
local ArmorSets = game.ServerStorage:WaitForChild("ArmorSets") 

local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()

local armorPieceTypeC0s = {
	Helmet = CFrame.new(0, 0, -0.6),
	Torso = CFrame.new(0, -0.18, -0.45),
	LowerTorso = CFrame.new(0, 0, 15.0),
	--etc.
}

local armorPieceTypeAttachmentNames = {
	Helmet = "FaceFrontAttachment",
	Torso = "BodyFrontAttachment",
	LowerTorso = "WaistRigAttachment",
	--etc.
}

function wearArmorPiece(armorPiece)
	local armorPieceType = armorPiece.Name
	local bodyAttachment = character:FindFirstChild(
		armorPieceTypeAttachmentNames[armorPieceType], true)
	local bodyPart = bodyAttachment.Parent

	armorPiece.Handle.Position = bodyAttachment.Parent.Position

	local weld = Instance.new("Weld")
	weld.Part0 = bodyPart
	weld.Part1 = armorPiece.Handle
	weld.C0 = armorPieceTypeC0s[armorPieceType]
	weld.C1 = bodyAttachment.CFrame
	weld.Parent = armorPiece

	armorPiece.Parent = character
end

function wearArmorSet(armorSet)
	for _, armorPiece in pairs(armorSet:GetChildren()) do
		local armorPiece = armorPiece:Clone()
		wearArmorPiece(armorPiece)
	end
end
1 Like