Bullet Not going towards mouse position

Im trying to make the bullet go towards my mouse’s position but it doesnt want to work

local function MakePart()
	local ray = Ray.new(tool.Nozzle.Position, tool.Nozzle.Position - mousePosition.Unit * 100)
	local rayhit,position = workspace:FindPartOnRayWithIgnoreList(ray,{plr.Character})
	local Bullet = Instance.new("Part")
	Bullet.BrickColor = BrickColor.new("Brick yellow")
	Bullet.Material = "Neon"
	Bullet.Parent = workspace
	Bullet.CanCollide = false
	Bullet.Anchored = true
	local Distance = (tool.Nozzle.Position - position).Magnitude
	Bullet.CFrame = CFrame.new(tool.Nozzle.Position, position) * CFrame.new(0,0,-Distance/2)
			
	Bullet.Size = Vector3.new(0.1,0.1,Distance)
	game:GetService("Debris"):AddItem(Bullet,.2)
end
MakePart()
4 Likes
(tool.Nozzle.Position - mousePosition).Unit * 100

You forgot the parenthesis around your subtraction.

2 Likes

Alright I tried that just now and its still not working

2 Likes

You could be incorrectly defining mousePosition, send the code.

2 Likes

Alright heres the code @iiNemo

Fire.OnServerEvent:Connect(function(plr,typ,tool,damage,mousePosition)
	if typ == "Semi" then
		local function MakePart()
			local ray = Ray.new(tool.Nozzle.Position, (tool.Nozzle.Position - mousePosition).Unit * 100)
			local rayhit,position = workspace:FindPartOnRayWithIgnoreList(ray,{plr.Character})
			local Bullet = Instance.new("Part")
			Bullet.BrickColor = BrickColor.new("Brick yellow")
			Bullet.Material = "Neon"
			Bullet.Parent = workspace
			Bullet.CanCollide = false
			Bullet.Anchored = true
			local Distance = (tool.Nozzle.Position - position).Magnitude
			Bullet.CFrame = CFrame.new(tool.Nozzle.Position,position) * CFrame.new(0,0,-Distance/2)
			
			Bullet.Size = Vector3.new(0.1,0.1,Distance)
			game:GetService("Debris"):AddItem(Bullet,.2)
		end
		MakePart()
	else
		print("Auto!")
	end
end)
1 Like

Your calculating the direction incorrectly, its this:

(mousePosition - tool.Nozzle.Position).Unit * 100

That didnt work either
30chars

send the part of the code when your firing the remote event.

function Gun.new()
	return {
		
		gunFire = function(self,typ,tool,damage,dbTime,hit)
			tool.Equipped:Connect(function()
				print("Equipped!")
			end)
			
			tool.Activated:Connect(function()
				if not debounce then
					debounce = true
					Fire:FireServer("Semi",tool,damage,hit)
					wait(dbTime)
					debounce = false
				end

			end)
		end
		
		
	}
end

Local Script

local GunHandler = require(game.ReplicatedStorage.GunHandler)
local tool = script.Parent
local plr = game.Players.LocalPlayer
local mouse = plr:GetMouse()

local var = GunHandler.new()
var:gunFire("Semi",tool,10,.5,mouse.Hit.Position)

You need to get the mouse position when the tool is activated, right now your passing the mouse position when the object is instantiated, hence it remains at a fixed location.

local mouse = game:GetService("Players").LocalPlayer:GetMouse()

tool.Activated:Connect(function()
	if not debounce then
		debounce = true
		Fire:FireServer("Semi",tool,damage,mouse.Hit.Position)
		wait(dbTime)
		debounce = false
	end
end)

I had a feeling this was somewhat of the problem, because I have passed along math.random through it and its worked before

@cjjdawg Lets me test this really quickly

@cjjdawg Thanks its working now.

1 Like

Why are u firing the server the damage?