How to tween a part to the mouses position?

So I want to tween a part into the mouse’s position, but I don’t know how to do it.

I was thinking of getting the mouse’s position then somehow tween it into that position you clicked at.

I don’t have any code because i’m still not sure how to do it, (and I am at school, using my free time to ask), its for a rocket launcher, and I am getting a part from ServerStorage and duplicate it into the workspace where the rocket launcher is. I can figure out the explosions myself.

Anything helps

well you wouldn’t necessarily want to tween for a rocker launcher or any gun-type system, but if you did you could do something along the lines of

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

mouse.ButtonOneDown:Connect(function()
      —[[ use a raycast to hitscan the projectile.
      you can use “mouse.Hit.Position” to send the         
      projectile in that direction]]—
end)

if you need more help check out this article about raycasting. or watch this video by TheDevKing. best of luck.

4 Likes

something like this should be working

local player = game.Players.LocalPlayer
local Mouse = player:GetMouse() --Deprecated but still functions

local MousePos = Vector3.new(Mouse.Hit.X, 0, Mouse.Hit.Z)
game:GetService("UserInputService").InputBegan:Connect(function(i, gpe)
	if not gpe and i.UserInputType == Enum.UserInputType.MouseButton1 then
		print("TWEENING")
		local INF = TweenInfo.new(
			1, 
			Enum.EasingStyle.Sine, 
			Enum.EasingDirection.In,
			0,
			false,
			0
		)
		local ENF = {["Position"] = MousePos}
		game:GetService("TweenService"):Create(game.Workspace.Part, INF, ENF):Play()
	end
end)
4 Likes
local run = game:GetService("RunService")
local tweens = game:GetService("TweenService")
local players = game:GetService("Players")
local player = players.LocalPlayer
local mouse = player:GetMouse()

local part = Instance.new("Part")
part.Anchored = true
part.Parent = workspace
mouse.TargetFilter = part

run.RenderStepped:Connect(function()
	local tween = tweens:Create(part, TweenInfo.new(0.2, Enum.EasingStyle.Linear, Enum.EasingDirection.Out), {Position = mouse.Hit.Position + Vector3.new(0, part.Size.Y/2, 0)})
	tween:Play()
end)

https://gyazo.com/3b4b5cd5819bda0c16d8f5d1332a2f3b

4 Likes

How do i send it into that direction? Thats what im trying to do

I’ll try it out. Thanks

eofiehfj more words how do i blur things btw

Its supposed to be serversided. How do i make it serversided?

I cant have more than 3 arguements in a variable.

Also i tried this out on another test script, this is kinda what I meant? But however, I was going for it to happen serversided, and I don’t want it following your mouse, I want it to move to where your mouse HIT. But I think I can fix that, but I am not sure

Not sure why its getting a part in workspace, I am cloning a part i got from serverstorage

You could also use Raycasting on UserInputService, which will work generally better as it will be able to work more efficiently with Mobile + PC players with more compatibility.

local Cam = workspace.CurrentCamera

local Parameters = RaycastParams.new()
Parameters.FilterDescendantsInstances = {} -- Place in here if there is a specific group you want to be filtered (whitelisted or blacklisted)
Parameters.FilterType = Enum.RaycastFilterType.Whitelist -- This will tell the raycast whether to whitelist or blacklist the filter.

local function RayResult(x, y)
      local unitRay = Cam:ScreenPointToRay(x, y) -- You could also use viewportpointtoray
      return workspace:Raycast(unitRay.Origin, unitRay.Direction * 500, Parameters)
end

-- You could then recall the position of it by doing

game:GetService("UserInputService").InputBegan:Connect(function(Input, Processed)
      if not Processed then
            if Input.UserInputType == Enum.UserInputType.Touch or Input.UserInputType == Enum.UserInputType.MouseButton1 then
            local Result = RayResult(Input.Position.X, Input.Position.Y)
            local Position = Result.Position -- This would then put the world space point where the mouse was into a variable as a Vector3, you can then use that to tweenposition easily.
            end
      end
end)

Any queries drop me a DM - Astral#0003

1 Like

Line 18 there was an error: attempt to index nil with ‘Position’ - Client - LocalScript:18

Adding to that, no clue what anything does :sweat_smile:

Try it now, I misspelled Parameters. lol

1 Like

I already fixed that part, I was talking about line 18.

Add me on Discord - Astral#0003 so I can take a look

Alright, I got piano so i will be back in like an hour or less.

Apologies, I’ve worked out what the issue is.

The script I posted I had limited to whitelist, but you hadn’t added anything into the descendants. I’ve changed the filter type to Blacklist here and added a if statement checking if the result exists before it carries out any task to reduce errors.

local Cam = workspace.CurrentCamera

local Parameters = RaycastParams.new()
Parameters.FilterDescendantsInstances = {} -- Place in here if there is a specific group you want to be filtered (whitelisted or blacklisted)
Parameters.FilterType = Enum.RaycastFilterType.Blacklist -- This will tell the raycast whether to whitelist or blacklist the filter.

local function RayResult(x, y)
	local unitRay = Cam:ScreenPointToRay(x, y) -- You could also use viewportpointtoray
	return workspace:Raycast(unitRay.Origin, unitRay.Direction * 500, Parameters)
end

-- You could then recall the position of it by doing

game:GetService("UserInputService").InputBegan:Connect(function(Input, Processed)
	if not Processed then
		if Input.UserInputType == Enum.UserInputType.Touch or Input.UserInputType == Enum.UserInputType.MouseButton1 then
			local Result = RayResult(Input.Position.X, Input.Position.Y)
			if Result then
				print(Result.Position)
			end
		end
	end
end)

Instead of printing the position you could fire it to the server for the server to act with any form of movement you need.

2 Likes