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

How to move part to mouse position?

Asked by 5 years ago
Edited 5 years ago

I've been trying to get a part to move from its current position to where the mouse position is when they click. I've tried multiple times to try to get it working but I just can't figure out how.

I am using a regular script.

Code:

local part = script.Parent 
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local mousePos = mouse.Hit 


local function onCLick()
    part.Velocity = Vector3.new(mousePos.P)
end

mouse.Button1Down:connect(onCLick)



Any help is appreciated.

EDIT: I want the part to go towards the position of the mouse, not teleport to the position of the mouse.

0
Why change the part's Velocity instead of Position? PreciseLogic 271 — 5y
0
If you want the part to move towards the mouse cursor use "(part.Position-mousePos.P).unit*speedInStudsPerSecond" as the velocity (you might need to make it negative)... If you want the part to teleport to the cursor use "part.Position = mousePos.P"... Also mousePos.P is already a Vector3... You are getting the position component of a CFrame value (mouse.Hit). Hexcede 52 — 5y
0
I do want it to move towards the mouse position not teleport. Also I tried using what you gave me it still won't work. DogeDark211 81 — 5y
0
Edited my answer. jackfrost178 242 — 5y

1 answer

Log in to vote
1
Answered by 5 years ago
Edited 5 years ago

So before we get into how to solve your problem, let's just quickly go over what you have here, right?

  • You can't get the Local Player from a Server Script

  • You can't get the Mouse from a Server Script

  • You tried to move the part by changing it's Velocity

  • mouse.Hit.p is a Vector3 value, so you don't need to put it inside of another Vector3 value

How to fix this

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

local part = Instance.new("Part") -- You can just change this to part.Parent like you were doing before.
    part.Parent = workspace
        part.Anchored = true
            mouse.TargetFilter = part -- The mouse will ignore the part so it doesn't end up above the mouse position

mouse.Button1Down:connect(function()
    part.Position = mouse.Hit.p -- mouse.Hit.p is a Vector3 already
    -- mouse.HIt.p is equivalent to Vector3.new(mouse.Hit.p.X,mouse.Hit.p.Y,mouse.Hit.p.Z)
end)

I'm not sure exactly what you're going for, but if you wanted everyone to be able to see this part, you could fire the server with a remote event on click, and you can pass in the part's position as the argument.

Link on Remote Events/Functions

EDIT

I misunderstood the question apparently, so i'll just leave this here.

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local part = script.Parent -- Just assuming there's something here based on your script.
local startPoint = part.CFrame.p -- This will be the firing point basically
local speed = 100

mouse.Button1Down:connect(function()
local lookPoint = mouse.Hit.p
local projectile = Instance.new("Part") -- Creating the projectile
    projectile.Parent = workspace
            projectile.CFrame = CFrame.new(startPoint,lookPoint) -- Projectile starts at "startPoint", while looking at "lookPoint"
            mouse.TargetFilter = projectile -- The mouse will ignore the projectile so it doesn't end up above the mouse position   

local BV = Instance.new("BodyVelocity") -- Creating body velocity
    BV.Parent = projectile -- parenting it to the projectile
        BV.MaxForce = Vector3.new(math.huge,math.huge,math.huge) -- Setting max speed
    BV.Velocity = projectile.CFrame.lookVector * speed -- It will fly in the direction the projectile is facing multiplied by the Speed
end)

The rest of the work is up to you, that means utilizing RemoteEvents.

0
The reason why I tried moving the part with it's velocity is because I want it to go towards the mouse position. Like how the bullets for all the guns work. DogeDark211 81 — 5y
0
Sorry I misunderstood your question, you should've made that more clear. Just a sec. jackfrost178 242 — 5y
0
I should've, sorry. DogeDark211 81 — 5y
0
Well thanks for the help but.. Can it somehow move by a 1 stud grid?? Just asking robert19735 17 — 4y
View all comments (3 more)
2
Hey Thank you, you helped me after 2 years. MirmirTheKedi 2 — 3y
0
yeah ikr Xyternal 247 — 2y
0
i need help. Where/how should i fire the event here? Xyternal 247 — 2y
Ad

Answer this question