Is there a way to make a part go where the mouse is pointing?

Hi, I’m trying to make a gun for a game. I’m trying to figure out how to stop the bullet from the gun from dropping down, and make it go where the mouse is at. Since I have no idea how I would do that, I looked at youtube tutorials as well as messing around with the script. I have tried and have not found a way to get it to work.
I am using a local script

local Gui = script.Parent:WaitForChild("ScreenGui")
local MaxAmmo = 4
local Ammo = 4
local WaitTime = 1
local StoredAmmo = 3
local ShootPart = script.Parent.ShootPart
script.Parent.Parent.Equipped:Connect(function(Mouse)
	        animation = game.Players.LocalPlayer.Character.Humanoid:LoadAnimation(script.Parent.Animation)
	   	   GuiClone = Gui:Clone()
	   	   GuiClone.Parent = game:GetService("Players").LocalPlayer.PlayerGui
	        animation:Play()
		   animation.Looped = true
end)
script.Parent.Parent.Activated:Connect(function(Mouse)
	  animation2 = game.Players.LocalPlayer.Character.Humanoid:LoadAnimation(script.Parent.Animation2)
	  GuiClone.Frame.TextLabel.Text = Ammo.."|"..StoredAmmo
	  GuiClone.Enabled = true
	  script.Parent.Sound:Play()
	  ammoCheck()
	  Ammo = Ammo - 1
	  shoot()
	  animation2:Play()
	  wait(WaitTime)
end)

script.Parent.Parent.Unequipped:Connect(function()
	 animation:Stop()
	 animation.Looped = false
	GuiClone.Enabled = false
end)
function reload()
	StoredAmmo = StoredAmmo - 1
	Ammo = 4
	if StoredAmmo < 1 then
		GuiClone.Frame.TextLabel.Text = "No more ammo"
		wait(1.5)
		animation:Stop()
		animation2:Stop()
		GuiClone:Destroy()
		script.Parent.Parent:Destroy()
	end
end
function ammoCheck()
	if Ammo < 1 then
		reload()
	end
end
function shoot()
	local Bullet = Instance.new("Part", workspace)
	Bullet.Size = Vector3.new(0.43, 0.13, 0.23)
	Bullet.BrickColor = BrickColor.new("New Yeller")
	Bullet.Material = Enum.Material.Neon
	Bullet.Position = ShootPart.Position
	local Force = Instance.new("BodyForce")
	for count =1,10 do
		wait()
		Bullet.CFrame = Bullet.CFrame + Vector3.new(Force)
	end
end

Right now I was just trying to get the bullet to move where the cursor is.

First of all, in your Shoot() function you multiplying a CFrame value with a Body force second since you using a LocalScript you can use Mouse.Hit.p property

1 Like

Yup! @SadWowow21 you may find example code helpful.
Below is a script for a laser pointer, by Roblox:

local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
 
-- grab local player
local localPlayer = Players.LocalPlayer
 
-- create beam
local beam = Instance.new("Beam")
beam.Segments = 1
beam.Width0 = 0.2
beam.Width1 = 0.2
beam.Color = ColorSequence.new(Color3.new(1, 0, 0))
beam.FaceCamera = true
 
-- create attachments
local attachment0 = Instance.new("Attachment")
local attachment1 = Instance.new("Attachment")
beam.Attachment0 = attachment0
beam.Attachment1 = attachment1
 
-- parent attachments to Terrain 
beam.Parent = workspace.Terrain
attachment0.Parent = workspace.Terrain
attachment1.Parent = workspace.Terrain
 
-- grab the mouse
local mouse = localPlayer:GetMouse()
 
-- connect to RenderStepped (update every frame)
RunService.RenderStepped:Connect(function()
 
	-- make sure the character exists
	local character = localPlayer.Character
	if not character then
		-- disable the beam
		beam.Enabled = false
		return
	end
 
	-- make sure the head exists
	local head = character:FindFirstChild("Head")
	if not head then
		-- disable the beam
		beam.Enabled = false
		return
	end
 
	-- enable the beam
	beam.Enabled = true
 
	-- define origin and finish
	local origin = head.Position
	local finish = mouse.Hit.p
 
	-- move the attachments 
	attachment0.Position = origin
	attachment1.Position = finish
end)

However, CFrame.p is deprecated. Use mouse.Hit.Position


This is a really bad way to make bullets. I would reccommend using either Heartbeat or Stepped, so you can use delta to make the bullet travel smooth and accurate.

3 Likes