This should work for you:
[LocalScript]
local model = workspace.Model
local camera = workspace.CurrentCamera
game:GetService("UserInputService").InputBegan:connect(function(inputObject)
if (inputObject.UserInputType ~= Enum.UserInputType.MouseButton1) then return end
local mousePosition = inputObject.Position
local mouseRayUnit = camera:ScreenPointToRay(mousePosition.X, mousePosition.Y)
local mouseRay = Ray.new(mouseRayUnit.Origin, mouseRayUnit.Direction * 1000)
local _, mouseTarget, _ = workspace:FindPartOnRayWithIgnoreList(mouseRay, {model})
model:MoveTo(mouseTarget)
end)
Let's dive into what this is doing.
First, we have this line:
local mouseRayUnit = camera:ScreenPointToRay(mousePosition.X, mousePosition.Y)
What that line does is take the position of the mouse on the screen and convert it into a unit Ray. (If you don't know what a ray is, you'll have to do some research.)
Next, we have this:
local mouseRay = Ray.new(mouseRayUnit.Origin, mouseRayUnit.Direction * 1000)
What that does is take the unit ray and extend it to be 1000 studs long. This code means that whatever is being placed can be, at maximum, 1000 studs away from the camera's position.
Finally, we have this:
local _, mouseTarget, _ = workspace:FindPartOnRayWithIgnoreList(mouseRay, {model})
What this does is attempt to find the first part that intersects with the ray. We extract the position of the part that was found and use that to position the model.
If you're using FilteringEnabled, you'll have to make some tweaks for this to show up for other clients.
You'll also have to add in the rest of the code that you need.
I hope this helps! Good luck.
7 comments
I am uncertain how that script works, could you dim it down a little
what really confuses me is the fact you have " local_,mouseTarget,_ = " is that an entire variable? what does _, do?
And what is Enum?
@scottmike0 I updated my answer. "_" is the conventional variable name for an unused variable.
Can you break down this part game:GetService("UserInputService").InputBegan:connect(function(inputObject) 05 if (inputObject.UserInputType ~= Enum.UserInputType.MouseButton1) then return end I have not used GetService since Insert service was blocked at first and then changed...
@scottmike0 All that does is fetch the UserInputService. It then checks if the type of input that just began was a left-mouse-click. If not, it doesn't continue with the rest of the script.
This does not work