Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Is there a better way to shorten this script?

Asked by 9 years ago
--Armour Script
print("Putting on Armour")

local plr = game.Workspace.AllianceScripter

--Torso
local p = Instance.new("Part")
p.Size = Vector3.new(2,2,1)
p.Name = "Armour"
p.TopSurface = "Smooth"
p.BottomSurface = "Smooth"
p.BrickColor = BrickColor.new("Mid gray")
p.Reflectance = 0.3
p.Parent = plr
plr.Torso.Transparency = 1

local w1 = Instance.new("Weld", plr)
w1.Part0 = w1.Parent.Torso
w1.Part1 = p
w1.C0 = CFrame.fromEulerAnglesXYZ(0,0,0)*CFrame.new(0,0,0)

--RightArm
local rightarm = p:Clone()
rightarm.Size = Vector3.new(1,3,1)
rightarm.Name = "ArmourRightArm"
rightarm.TopSurface = "Smooth"
rightarm.BottomSurface = "Smooth"
rightarm.BrickColor = BrickColor.new("Mid gray")
rightarm.Reflectance = 0.3
rightarm.Parent = plr
rightarm.Transparency = 1

local w2 = Instance.new("Weld", plr)
w2.Part0 = plr["Right Arm"]
w2.Part1 = rightarm
w2.C0 = CFrame.fromEulerAnglesXYZ(0,0,0)*CFrame.new(0,0,0)

--LeftArm
local leftarm = p:Clone()
leftarm.Size = Vector3.new(1,3,1)
leftarm.Name = "ArmourRightArm"
leftarm.TopSurface = "Smooth"
leftarm.BottomSurface = "Smooth"
leftarm.BrickColor = BrickColor.new("Mid gray")
leftarm.Reflectance = 0.3
leftarm.Parent = plr
leftarm.Transparency = 1

local w3 = Instance.new("Weld", plr)
w3.Part0 = plr["Left Arm"]
w3.Part1 = leftarm
w3.C0 = CFrame.fromEulerAnglesXYZ(0,0,0)*CFrame.new(0,0,0)

1 answer

Log in to vote
3
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
9 years ago

There are a few things.

  • Instance.new has a second parameter; the parent.
local obj = Instance.new(class)
obj.Parent = model
-- becomes
local obj = Instance.new(class, model)
  • CFrame.Angles is the same thing as CFrame.fromEulerAnglesXYZ.

  • Multiplying by CFrame.new(0, 0, 0) is redundant; CFrame.new(0, 0, 0) is the identity -- multiplying by it changes nothing.

  • Coincidentally, CFrame.Angles(0, 0, 0) is also the identity, and the default value for C0 on snaps; so that line can be omitted.

This trims things down slightly, but not much.


We still have more or less the same code repeated, so we should use a function. These things change between them:

  • Part0 of weld
  • Size of part

Thus, those will be the parameters to our function, and everything else is as we had it:

-- Base: What our armor is based on.
-- A shiny brick.
local base = Instance.new("Part")
base.TopSurface, base.BottomSurface = "Smooth", "Smooth"
base.Reflectance = 0.3
base.BrickColor = BrickColor.new("Mid gray")
base.FormFactor = "Custom"

-- Puts a copy of "base" of size "size" attached to "part",
-- and hides "part".
function armor( part, size )
    local p = base:clone()
    p.Parent = part.Parent
    p.Size = size
    p.Name = "Armor" .. part.Name
    local w = Instance.new("Weld", part)
    w.Part0 = part
    w.Part1 = p
    part.Transparency = 1
end

armor( plr.Torso , Vector3.new(2, 2, 1))
armor( plr["Left Arm"], Vector3.new(1, 3, 1))
armor( plr["Right Arm"], Vector3.new(1, 3, 1))
Ad

Answer this question