What do you want to achieve?
Idk how to explain this but I have 2 scripts that if you push a button it will move the current piece left & right by using moveto(). my problem is that if you click the button the current piece moves right & left but it also moves the current piece slightly up and idk what making it do that
What is the issue?
The current piece moves slightly up when I move it left or right, but the piece only seems to start to move up when I move it right
What solutions have you tried so far?
idk where to start cause it should work, as well as the piece only starts to move up if I move it right
Just note that the move right script is basically the same but instead is -8 not +8
local currentPiece = workspace.CurrentPiece:GetChildren()[1]
script.Parent.MouseButton1Click:Connect(function()
local currentPieceX = currentPiece:GetPivot().X
local currentPieceY = currentPiece:GetPivot().Y
local currentPieceZ = currentPiece:GetPivot().Z
currentPiece:MoveTo(Vector3.new(
currentPieceX + 8,
currentPieceY,
currentPieceZ
))
print("moved left")
end)
any help or explanation of what’s going on is super helpful and meaningful to me. And I hope I’m not asking too much or that the fix is a lot of work. Lol
As @Kanglim12 already said, you need to use PivotTo. Here is an example:
local currentPiece = workspace.CurrentPiece:GetChildren()[1]
script.Parent.MouseButton1Click:Connect(function()
currentPiece:PivotTo(CFrame.new(currentPiece:GetPivot().Position+Vector3.new(8,0,0))) --move 8 in x
print("moved left")
end)
Your issue may be caused by floating-point inaccuracies or other factors affecting the position of the piece. To fix this issue, you can create a new function that moves the piece only along the X-axis while maintaining its Y and Z positions.
Here is my version of the script:
local function movePieceX(deltaX)
local currentPiece = workspace.CurrentPiece:GetChildren()[1]
local currentPosition = currentPiece:GetPivot()
local newPosition = currentPosition + Vector3.new(deltaX, 0, 0)
currentPiece:MoveTo(newPosition)
end
script.Parent.MouseButton1Click:Connect(function()
movePieceX(8)
print("moved left")
end)
Now, if you need to move the piece to the right, you can call the movePieceX() function with a negative value:
movePieceX(-8) -- Moves the piece to the right
If that did not work you can use the PivotTo function
local function movePieceX(deltaX)
local currentPiece = workspace.CurrentPiece:GetChildren()[1]
local currentPosition = currentPiece:GetPivot()
local newPosition = currentPosition + Vector3.new(deltaX, 0, 0)
currentPiece:PivotTo(newPosition)
end
script.Parent.MouseButton1Click:Connect(function()
movePieceX(8)
print("moved left")
end)
Yayyy it works, thank you so much. I tried @Kanglim12 solution and it gave me an error about casting a vector3 onto a coordinate frame. but then I tried yours and now it works.