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

Why do parts follow my mouse when I when I click it but not a model?

Asked by 5 years ago
Edited 5 years ago
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local mtarget
local down



function clickObj()
    if mouse.Target ~= nil and mouse.Target.Name == "model" then
        mtarget = mouse.Target
        print(mtarget)
        mouse.TargetFilter = mtarget
        print(mouse.TargetFilter)
        down = true
    end
end
mouse.Button1Down:connect(clickObj)


function mouseMove()
    if down and mtarget then
        local posX,posY,posZ = mouse.Hit.X,mouse.Hit.Y,mouse.Hit.z

        mtarget.Position = Vector3.new(posX,posY,posZ)
    end
end
mouse.Move:connect(mouseMove)


function mouseDown()
    down = false
    mouse.Target = nil
    mouse.TargetFilter = nil
end
mouse.Button1Up.connect(mouseDown)

1 answer

Log in to vote
0
Answered by
T1mes 230 Moderation Voter
5 years ago
Edited 5 years ago

The Model class does not have a Position value

you would have to use the function Model:MoveTo()

also use ClassName instead of name when checking for models so you don't have to to name all of your models "model"

also use :Connect() not :connect()

fixed version should look like this:

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



function clickObj()
    if mouse.Target ~= nil and mouse.Target.Parent.ClassName == "Model" then -- Changed name to ClassName
        mtarget = mouse.Target.Parent
        print(mtarget)
        mouse.TargetFilter = mtarget
        print(mouse.TargetFilter)
        down = true
    end
end
mouse.Button1Down:Connect(clickObj)


function mouseMove()
    if down and mtarget then
        local posX,posY,posZ = mouse.Hit.X,mouse.Hit.Y,mouse.Hit.z

        mtarget:MoveTo(Vector3.new(posX,posY,posZ)) -- Added the function
    end
end
mouse.Move:connect(mouseMove)


function mouseDown()
    down = false
    mtarget = nil
    mouse.TargetFilter = nil
end
mouse.Button1Up:Connect(mouseDown)

Ad

Answer this question