Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How can I move a model only on one axis?

Asked by
Seyfert 90
6 years ago
Edited 6 years ago

I am creating a tool where players can move different furniture around the map by left clicking on the furniture and then placing the furniture by moving their mouse to their desired location and letting go of the left click.

I ONLY want the furniture to move on the X axis but for some reason it is changing the rotation of the model as well. How can I make it only move on the X and/or Z axis and NOT affect rotation?

My code in a LocalScript

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local moveEvent = nil --We need to store the event so we may disconnect when left-click is released
local firstPosition = nil --Initialize to later store the brick's position
local finalPosition = nil --Initialize to later store the brick's moving position
local target = nil

mouse.Button1Down:connect(function()
    target = mouse.Target
    if target and target.Parent.Name == "Furniture" then
        --target.Parent.PrimaryPart = target --Each model will have a target beforehand
        mouse.TargetFilter = target
        firstPosition = target.Position
        --print('firstPosi is', firstPosition)      
        moveEvent = mouse.Move:connect(function()   
        --Nothing here as of now, placeholder          
        end)
    end
end)

mouse.Button1Up:connect(function()
    finalPosition = mouse.Hit.p 
    --print('mouse up first pos', firstPosition)
    --print('final minus first pos', finalPosition.x - firstPosition.x) 
    --print('final minus first pos ABS', math.abs(finalPosition.x - firstPosition.x))
    if math.abs(finalPosition.x - firstPosition.x) > 2 then --The position of the first click compared to when the player lets go of the left click has to be more than 2 studs or whatever
        print('yes')
        --target.Parent.PrimaryPart.Position.x = finalPosition.x --Only moves the individual part and not the whole model so this does not work D:
        --target.Parent.PrimaryPart.Position.z = finalPosition.z --Same issue but on z axis     
        target.Parent:SetPrimaryPartCFrame(mouse.Hit)       
    end     
end) 

Answer this question