I’m making models (different fruits) weld onto a chopping board model inside my character. I replicated their position from the original chopping board onto this cloned one using a replica of what it should look like inside of ServerStorage. How would I also calculate the angle to rotate the fruits correctly while using a weld?
CFruit is a model on the chopping board in Workspace
SFruit is a model on the chopping board in ServerStorage (the example board I’m replicating)
SChopping is the chopping board in ServerStorage
Here is what has been so far successful for one of the fruits, but the banana gets flipped upside down, so I’m having trouble finding the true calculations.
local CFruit = SFruit:Clone()
WeldModel(CFruit) --Welds it together if there are multiple parts.
local X, Y, Z = SFruit.PrimaryPart.CFrame:ToEulerAnglesXYZ() --Gets the original angle
CFruit.Parent = ChoppingBoard:WaitForChild("Fruits")
CFruit.PrimaryPart.CFrame = ChoppingBoard:WaitForChild("Board").CFrame * CFrame.Angles(X, Y, Z) --idk if the weld overwrites, probably does
SetVisibility(CFruit, 0) --make it visible
CFruit.PrimaryPart.Anchored = false
local Weld = Instance.new("Weld")
Weld.Part0 = ChoppingBoard:WaitForChild("Board")
Weld.Part1 = CFruit.PrimaryPart
local X1, Y1, Z1 = SFruit.PrimaryPart.CFrame:ToEulerAnglesXYZ() --gets the original angle (on server example)
local X2, Y2, Z2 = SChopping.PrimaryPart.CFrame:ToEulerAnglesXYZ() --gets the original chopping board angle (on server example)
local Difference = CFrame.new(SChopping.Board.Position - SChopping.Fruits[Fruit.Parent.Name].PrimaryPart.Position) * CFrame.Angles(X2 - X1, Y2 - Y1, (Z2 - Z1) * math.rad(180)) --Gets the difference in the position and then tries to in the angles. The board was rotated 180 degrees on the Z axis as well.
Weld.C0 = Difference
Weld.Parent = CFruit
WeldModel(CFruit) --Welds it together if there are multiple parts.
local X, Y, Z = SFruit.PrimaryPart.CFrame:ToEulerAnglesXYZ() --Gets the original angle
CFruit.Parent = ChoppingBoard:WaitForChild("Fruits")
CFruit.PrimaryPart.CFrame = ChoppingBoard
WeldModel has come first then you CFramed it CFrame first then Weld it.
The reason for me applying the 180 roatation was because that’s what I did to the board when welding it to my character. I’m not sure if this is how I even do it, it’s just an attempt.
Still, doesn’t work. I’ve played around and it works for some of the fruits, but not the others. I want to find a solution that will work for all fruits for easy configuration later on.
So what you can do is save the orientation of your fruit to the chopping board on initialization.
Like if your SFruit is already oriented correctly to SChopping, you can save it using this method:
local newCFrame = SChopping.Board.CFrame:Inverse() * SFruit.PrimaryPart.CFrame
Then you can set the C0 of the Weld between the real chopping board and fruit to newCFrame:
Weld.C0 = newCFrame
How you store this CFrame is up to you.
I may also be wrong, but I’m pretty sure this is the answer if you already have it all set up.
Could it be that you could be holding the chopping board upside-down? Because it looks like that’s what’s happening. It would make a lot of sense too actually.
Try just flipping SChopping in server storage upside-down and try again with my original suggestion.
I like the way the board is currently welded. It was rotated because I wanted the board to be flipped the correct way. There’s a hole on the left side, but when I originally welded it, the hole was on the right. Therefore, I rotated it 180 degrees on the Z axis.
My guess is that you need to rotate the board on one of the other axes to orient it correctly, because the board was flipped upside-down while getting the hole on the right side.
Try flipping the board on the Y-axis instead: