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