Why doesn't my gun shoot the bullet the right way?

basically I want to make the bullet shoot in the right direction with the orientation going the right way to and not side ways no matter where I’m looking, I am not exactly sure what to do with this.

Here is the codes I have,

LocalScript (located in gun tool) :

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

mouse.Button1Down:Connect(function()
	script.Parent.Shot:FireServer(mouse,mouse.Target)
end)

Script (located in gun tool) :

local FirePart = script.Parent.FirePart

local speed = 100

local player = game.Players:GetPlayerFromCharacter(script.Parent.Parent)

function CreateBullet(mouse, target)
	local Bullet = game.ReplicatedStorage:WaitForChild("Bullet"):Clone()
	Bullet.Parent = workspace
	Bullet.CFrame = FirePart.CFrame
	Bullet.Velocity = CFrame.new(FirePart.Position, target.Position).lookVector * 100
end

script.Parent.Shot.OnServerEvent:Connect(function(plr,mouse, target)
	CreateBullet(mouse, target)
	if not target:IsDescendantOf(plr.Character) then
		if target.Parent:FindFirstChild("Humanoid") then
			if target.Name == "Head" then
				target.Parent.Humanoid.Health -= 20
			else
				target.Parent.Humanoid.Health -= 10
			end
		end
	end
end)

Video of problem:

2 Likes

You’re Using The Target Position, Not Mouse.Hit.Position,
Change This:

function CreateBullet(mouse, target)
	local Bullet = game.ReplicatedStorage:WaitForChild("Bullet"):Clone()
	Bullet.Parent = workspace
	Bullet.CFrame = FirePart.CFrame
	Bullet.Velocity = CFrame.new(FirePart.Position, target.Position).lookVector * 100
end

To This:

function CreateBullet(mouse, target)
	local Bullet = game.ReplicatedStorage:WaitForChild("Bullet"):Clone()
	Bullet.Parent = workspace
	Bullet.CFrame = FirePart.CFrame
	Bullet.Velocity = CFrame.new(FirePart.Position, mouse.Hit.Position).lookVector * 100
end
1 Like

How do I get the players mouse from the “Script” and not “LocalScript” ?

1 Like

You Cant, But What You Can Do Is Sent The Mouse Position To Server And Change Velocity To That

I figured it out, Also what code can I put for the orientation of the bullet it’s self to make it look more realistic and not go sideways?

2 Likes

Change The FirePart CFrame, You’re Using The CFrame Of It For The Bullet CFrame, And If You Want To (Optional): You Can Use FastCast Module To Make The Bullet Speed Constant, You Can Make The Bullet Size Smaller So Its More Realistic And Also You Can Add A Trail So It Look Nice, If You Dont Know How To Use Fastcast You Can See This Tutorial

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.