I want to tween all the parts of a model to a certain position while keeping their relative position to the model and their shape. Although the parts tween successfully, they do not retain their shape.
I have attempted welding, ToObjectSpace, and subtracting the model’s position by the part’s position.
Whats happening in game:
Here’s my code:
function placementModule.Start(sentModel)
-- Check if the player is already placing something
if currentlyPlacing then
-- If so, return an error message and exit the function
error('Error 001: The player is already placing an object. If you believe this is incorrect, please file a report with the error code.')
return
end
-- If the player is not currently placing anything, set the currentlyPlacing variable to true
currentlyPlacing = true
-- Check if the desired model exists in ReplicatedStorage
local repStorage = game:GetService('ReplicatedStorage')
if not repStorage.models:FindFirstChild(sentModel) then
-- If it doesn't, return an error message and exit the function
error('Error 002: No model in rep stor. If you believe this is incorrect, please file a report with the error code.')
return
end
-- Clone the desired model from ReplicatedStorage to the Workspace
local newModel = repStorage.models[sentModel]:Clone()
newModel.Parent = game.Workspace
newModel.PrimaryPart = newModel:FindFirstChild('Hitbox')
-- Begin a loop that will run as long as the player is still placing the model
while currentlyPlacing do
-- Wait for the next RenderStepped event
game:GetService('RunService').RenderStepped:Wait()
-- Use a function called startRaycast to perform a raycast and get the result
local rayResult = placementModule.startRaycast(newModel)
-- If the raycast hit something, check if it hit the designated placement area
if rayResult and rayResult.Instance == game.Workspace.placementArea then
-- Calculate the position of the model in grid units
local gridPos = Vector3.new(
math.floor(rayResult.Position.X / gridStud) * gridStud,
rayResult.Position.Y + (newModel.Hitbox.Size.Y / 2),
math.floor(rayResult.Position.Z / gridStud) * gridStud
)
local gridCFrame = CFrame.new(gridPos)
-- If the model is outside the grid boundaries, do nothing
if math.abs(gridPos.X) > 44 or math.abs(gridPos.Z) > 44 then
--Do nothing
end
-- Otherwise, move the Hitbox to the calculated grid position
newModel.Hitbox.Position = gridPos
-- Loop through all the parts of the model and tween their positions to the grid position
for _, part in pairs(newModel:GetDescendants()) do
if part:IsA('Part') then
part.CanCollide = false
if part.Name ~= 'Hitbox' then
local tweenInfo = TweenInfo.new(
0.2,
Enum.EasingStyle.Linear,
Enum.EasingDirection.InOut
)
local tween = game:GetService('TweenService'):Create(part, tweenInfo, {Position = gridPos})
tween:Play()
end
end
end
end
end
end