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

how to grab an object with the mouse?

Asked by 5 years ago
Edited 5 years ago

I want to make a model move with the mouse but I do not know how I know just to make it take only one part I want it to take the whole model I tried with the primary part of the model more did not work. Please, can someone help me

local player= game.Players.LocalPlayer
local mouse = player:GetMouse()
local point 
local down


function clickObject()
 if mouse.Target ~= nil and mouse.Target.Name ~= "Baseplate" then
  point=mouse.Target
  mouse.TargetFilter = point
  down=true
 end
end

mouse.Button1Down:Connect(clickObject)

function moveMouse()
 if down and point then
  local posX, posY, posZ = mouse.Hit.X,mouse.Hit.Y,mouse.Hit.Z
  point.Position = Vector3.new(posX,posY,posZ)
 end
end

mouse.Move:Connect(moveMouse)

function mouseDown()
 down = false
 point = nil
 mouse.TargetFilter = nil
end

 mouse.Button1Up:Connect(mouseDown)

-- this was the script that I did more, I want to improve it so I do not get only parts, but the whole model

I edited, I stay like this but it did not work

local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()
local ModelPlacing = false
local Picked, PickedsOldParent
Mouse.TargetFilter = game.Workspace.CurrentCamera

function KeyDown()
    local Picked = Mouse.Target.Parent --Parent since we are trying with a model.
    --At this point you have to find out if Picked is a legit model to move. It may not be something what you want to move. Beware.

    PickedsOldParent = Picked.Parent
    Picked.Parent = game.Workspace.CurrentCamera
    ModelPlacing = true
end

function KeyUp()
    if Picked then
        Picked.Parent = PickedsOldParent
        Picked = nil
        --This is where you need to fire a RemoteEvent to make it FE compatible.
    end
    if ModelPlacing then
        ModelPlacing = false
    end
end

function MouseMove()
    if Picked and ModelPlacing then
        --First, let's get position.
        local Pos = Mouse.Hit.X,Mouse.Hit.Y,Mouse.Hit.Z

        --Then model's rotation in CFrame shape. For that, we need to substract position from CFrame.
        local RotCF = Picked.PrimaryPart.CFrame - Picked.PrimaryPart.CFrame.new(Pos)

        --Now time to get those together.
        Picked:SetPrimaryPartCFrame(CFrame.new() * CFrame.new(Pos))
        --Pos was Vector3, with CFrame.new, we turned it to CFrame position. RotCF was already a CFrame without a position.
    end
end

Mouse.Button1Down:Connect(KeyDown)
Mouse.Button1Up:Connect(KeyUp)
Mouse.Move:Connect(MouseMove)
0
If you tried, you should post your code. User#19524 175 — 5y
0
Post your code, And then I will help you. This is not a request site. seith14 206 — 5y
0
you can try welds Zendaya774isstupid 50 — 5y
0
Post your code or you question looks like a request. NOT A REQUEST SITE! OBenjOne 190 — 5y
0
Soon this was the script I did not put before because I had a problem Madtraxx_YT 2 — 5y

2 answers

Log in to vote
1
Answered by 5 years ago

Alright so as I see you are trying to do an object moving script. Please note that since there is no experimental mode on, the model you're placing will only be visible for the player as long as you do not fire a RemoteEvent to place the furniture on ServerScript. You can ask for that later.

What you are missing is the function :SetPrimaryPartCFrame() but I will start telling from the start to let you understand better.

Let's get player and the mouse first. Then the variables. I'll add Camera in TargetFilter just in case.

local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()
local ModelPlacing = false
local Picked, PickedsOldParent
Mouse.TargetFilter = game.Workspace.CurrentCamera

Now let's do Button1Down and Up process. We will move model to camera. We have camera in filter so mouse will ignore model when it's placed.

function KeyDown()
    local Picked = Mouse.Target.Parent --Parent since we are trying with a model.
    --At this point you have to find out if Picked is a legit model to move. It may not be something what you want to move. Beware.

    PickedsOldParent = Picked.Parent
    Picked.Parent = game.Workspace.CurrentCamera
    ModelPlacing = true
end

function KeyUp()
    if Picked then
        Picked.Parent = PickedsOldParent
        Picked = nil
        --This is where you need to fire a RemoteEvent to make it FE compatible.
    end
    if ModelPlacing then
        ModelPlacing = false
    end
end

Let's do the moving thing, which you wanted.

function MouseMove()
    if Picked and ModelPlacing then
        --First, let's get position.
        local Pos = Mouse.Hit.p

        --Then model's rotation in CFrame shape. For that, we need to substract position from CFrame.
        local RotCF = Picked.PrimaryPart.CFrame - Picked.PrimaryPart.CFrame.p

        --Now time to get those together.
        Picked:SetPrimaryPartCFrame(CFrame.new(Pos) * RotCF)
        --Pos was Vector3, with CFrame.new, we turned it to CFrame position. RotCF was already a CFrame without a position.
    end
end

Let's do not forget to connect.

Mouse.Button1Down:Connect(KeyDown)
Mouse.Button1Up:Connect(KeyUp)
Mouse.Move:Connect(MouseMove)

Please note that I didn't test it. Contact to me through comments or PM from my profile to discuss and require more help. If this helped, please upvote and accept as answer.

Ad
Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

Was this

local player= game.Players.LocalPlayer
local mouse = player:GetMouse()
local point 
local down


function clickObject()
 if mouse.Target ~= nil and mouse.Target.Name ~= "Baseplate" then
  point=mouse.Target
  mouse.TargetFilter = point
  down=true
 end
end

mouse.Button1Down:Connect(clickObject)

function moveMouse()
 if down and point then
  local posX, posY, posZ = mouse.Hit.X,mouse.Hit.Y,mouse.Hit.Z
  point.Position = Vector3.new(posX,posY,posZ)
 end
end

mouse.Move:Connect(moveMouse)

function mouseDown()
 down = false
 point = nil
 mouse.TargetFilter = nil
end

 mouse.Button1Up:Connect(mouseDown)

-- this was the script that I did more, I want to improve it so I do not get only parts, but the whole model    

Answer this question