Why is my bullet not flying to cursor?

I’ve made this short projectile based gun but for some reason the bullet just flies to the center of the world, I have no clue why this is happening.

Server:

local Remotes = game.ReplicatedStorage.GunEvents
local Shoot = Remotes.Shoot

local IgnoreTable = {
	game.Workspace.Bullets;	
}



Shoot.OnServerEvent:Connect(function(plr, Barrel, Stat, Handle, MousePos, MouseCFrame)	
	local BarrelPos = Barrel.Position
	
	print(plr.Character)
	
	local Hit, Pos = game.Workspace:FindPartOnRayWithIgnoreList(Ray.new(plr.Character.Head.Position, (MousePos - plr.Character.Head.Position)), {plr.Character})
	
	local Bullet = game.ReplicatedStorage.Bullet:Clone()
	Bullet.CFrame = CFrame.new(BarrelPos, Pos) * CFrame.new(0, 0, -2);

	local bv = Instance.new("BodyVelocity", Bullet)
	bv.MaxForce = Vector3.new(math.huge,math.huge,math.huge)
	bv.Velocity = MouseCFrame.LookVector * 1;
	
	Bullet.Parent = game.Workspace.Bullets
end)

Client:

local Stat = {

Damage = 10;

Spread = 5;

Mode = 'Auto';

}

script.Parent.Equipped:Connect(function()

Hold:Play()

end)

script.Parent.Activated:Connect(function()

Hold:Stop()

Aim:Play()

Aim.Stopped:wait()

Aimed:Play()

Shoot:FireServer(Barrel, Stat, Handle, MousePos, MouseCFrame)

end)

Some passed on variables are defined, they just are self explanatory.

2 Likes

Is the mousePos mouse.hit.p or is it the cframe one?

I believe you haven’t fired the proper parameter to the server, which is the MousePos. Replace that with the aforementioned statement: mouse.Hit.p

this is my mouse pos stuff

local plr = game.Players.LocalPlayer
local Mouse = plr:GetMouse()
local MousePos = Mouse.Hit.p
local MouseCFrame = Mouse.Hit

Mouse.Hit is the position where the mouse is hitting, not the direction, you should instead use Bullet.CFrame.LookVector

Would I replace the part where I set bullet’s CFrame with this

No, the part where you do bv.Velocity

But the bullet velocity is working it just faces wrong direction

Yeah I know set it to bv.Velocity = Bullet.CFrame.LookVector * Velocity
Replace Velocity with a velocity for your bullet

Still fires in other direction https://gyazo.com/8e629c47c9869759432e703909819615

Shoot.OnServerEvent:Connect(function(plr, Barrel, Stat, Handle, MousePos, MouseCFrame)	
	local BarrelPos = Barrel.Position
	
	print(plr.Character)
	
	local Hit, Pos = game.Workspace:FindPartOnRayWithIgnoreList(Ray.new(plr.Character.Head.Position, (MousePos - plr.Character.Head.Position)), {plr.Character})
	
	local Bullet = game.ReplicatedStorage.Bullet:Clone()
	Bullet.CFrame = CFrame.new(BarrelPos, MouseCFrame.Position);

	local bv = Instance.new("BodyVelocity", Bullet)
	bv.MaxForce = Vector3.new(math.huge,math.huge,math.huge)
	bv.Velocity = MouseCFrame.LookVector * 1;
	
	Bullet.Parent = game.Workspace.Bullets
end)

Fixed it, issue was I defined the mouse position early in the script meaning it stayed constant

1 Like