Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How would I move a model to the position of where my mouse is?

parent = script.Parent

function onButton1Down(mouse)

	local model = parent.Wall:clone()

	
	model:MakeJoints()- creates the pieces
	model.Parent = game.Workspace-- places it in workspace
	model:MoveTo(mouse,mouse,mouse)--sets the location of mouse
	wait(10)
	workspace.Wall:Destroy()
end

 --- I am confused what to do from here
--Okay this is the revised script
parent = script.Parent
local Player = game.Players.LocalPlayer
local mouse = Player:GetMouse()
local wall = parent.Wall:clone()

mouse.Button1Down:connect(function()
	wall:MakeJoints()
	wall.Parent = game.Workspace
	wall:MoveTo(mouse,0,mouse)
	wait(10)
	wall:Destroy()
	print("works")

end)

---and still nothing 

2 answers

This should work for you:

[LocalScript]

local model = workspace.Model -- this should be the model you want to move
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

0

I am uncertain how that script works, could you dim it down a little

0

what really confuses me is the fact you have " local_,mouseTarget,_ = " is that an entire variable? what does _, do?

0

And what is Enum?

0

@scottmike0 I updated my answer. "_" is the conventional variable name for an unused variable.

0

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...

0

@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.

0

This does not work

--- The best answer to this question is to actually make the part through coding
--- the above answer is incorrect in my eyes, because it fails to function when testing in roblox studios
--- Also, I love to use hopperbins.

This discussion is preserved as it was. New answers, comments, and votes are closed.