How would I be able to correctly code this function?

Hi there,

Looking for some assistance.

I was advised to create a function because the part’s I was creating had the same properties except for the color and size. I’m having a hard time trying to explain the issue, basically I want it each part to be labeled, part1, part2, part3 … etc. I hope the code could explain itself

local tweenService = game:GetService("TweenService")
local tweenInformation = TweenInfo.new(
	5,
	Enum.EasingStyle.Quint,
	Enum.EasingDirection.InOut,
	0,
	false,
	0
)
local Part = Instance.new("Part") 
function createPart(color)
	Part.Parent = game.Workspace
	Part.Size = Vector3.new(1,1,1)
	Part.Anchored = true
	Part.CanCollide = false
	Part.Material = Enum.Material.SmoothPlastic
	Part.BrickColor = color
end

createPart(BrickColor.Black) 
createPart(BrickColor.DarkGray()) -- I don't want it to overwrite the other createPart

local partInfo1 = { 
	Size = Vector3.new(2.5,0.3,2.5)
}

local partInfo2 = { 
	Size = Vector3.new(2.7,0.2,2.7);
}

local tween1 = tweenService:Create(createPart, tweenInformation, partInfo1)
local tween2 = tweenService:Create(createPart, tweenInformation, partInfo2)
wait(10)
tween1:Play()
tween2:Play()

Appreciate it,
Naucify

Inside here, you want to run Part:Clone() and store the result as a variable and change the new part’s properties. Another small thing is to set the parent of the part last so the server doesn’t have to replicate as much data.

2 Likes

Hi there,

Thanks for the helpful insight, when I run the code I get no errors but nothing happens? If you wish, could you elaborate?

Appreciate it,
Naucify

Try to put the Instance.new() inside the create function like this:


function createPart(color)
    local Part = Instance.new("Part")
 	Part.Parent = game.Workspace
 	Part.Size = Vector3.new(1,1,1)
 	Part.Anchored = true
 	Part.CanCollide = false
 	Part.Material = Enum.Material.SmoothPlastic
 	Part.BrickColor = color
end

and remove the arcs from DarkGray()

1 Like