Introduction
Greetings fellow developers, recently I was looking for a system to animate constructing models for use in a game I’m working on. I couldn’t find anything that met my needs, so I created this. Hopefully it helps someone else too!
What is ModelAssembler?
ModelAssembler uses TweenService to animate the parts in a model to “build” it. Models (or parts) are placed in a location such as ServerStorage, and ModelAssembler will construct it in workspace. Below is an example using a picnic bench.
Usage
To use ModelAssembler, first grab the model below and insert it into your game, preferably in ServerScriptService.
Once the module is in your game, require it from the script you wish to use it in.
local ModelAssembler = require(game:GetService("ServerScriptService").ModelAssembler)
To create a new ModelAssembler object, call the .new() method and pass the model that you wish to build. Make sure that your model has a primary part, you’ll receive an error if it doesn’t.
local assembler = ModelAssembler.new(model)
To create the building animation, use the :Build() method and supply both the CFrame where the model is going to be built and the speed in seconds it will take for the entire model to be built.
assembler:Build(CFrame.new(), 5)
Documentation:
-- Methods
ModelAssembler:BuildOnce(model, cframe, speed) -- Builds the model supplied once, cleans up afterwards
assembler = ModelAssembler.new(model) -- Creates a new ModelAssembler object
assembler:Build(cframe, speed) -- Builds the model previously attached, can be called multiple times
assembler:Destroy() -- Destroys the assembler
-- Properties
assembler.Model -- Model
assembler.InstanceAdded -- Signal
assembler.Completed -- Signal
assembler.FlyInFrom -- CFrame
assembler.StartingTransparency -- Number
Examples:
local ModelAssembler = require(game.ServerScriptService.ModelAssembler)
local assembler = ModelAssembler.new(game.ServerStorage.Bench)
assembler.InstanceAdded:Connect(function(inst)
assembler.FlyInFrom = CFrame.new(Vector3.new(math.random(0, 5), math.random(0, 5), math.random(0, 5))) * CFrame.Angles(math.random(math.pi*2), math.random(math.pi*2), math.random(math.pi*2))
end)
assembler.Completed:Connect(function(model)
print(model)
end)
assembler:Build(CFrame.new(Vector3.new(0, 10, 0)), 10)
ModelAssembler:BuildOnce(game.ServerStorage.Bench, CFrame.new(Vector3.new(10, 5, 0)) * CFrame.Angles(0, math.rad(90), 0), 10)
Conclusion
Thanks for reading! I hope this is beneficial to your game. Feel free to give suggestions, I plan to actively maintain this system and add new features.
Special thanks to @steven4547466 for helping convert my original code to OOP as well as adding some new features. The original module is below if you would like a simpler version or you’re not as advanced in programming.
ModelAssembler Original