Bullet wont go in direction of mouse (solved :D)

I have a server script that creates a bullet for a gun, and then ejects it using bodyvelocity and look vector. The bullet keeps going in the same direction though, and i have no errors in the output. What could i be doing wrong?

local event = game.ReplicatedStorage.GunEvents.Bullet
local tool = script.Parent.Parent
function onEvent(player, shotsound, hit)
	shotsound:Play()
	local bullet = Instance.new("Part")
	bullet.Shape = Enum.PartType.Block
	bullet.Color = Color3.new(0.694118, 0.462745, 0)
	bullet.Material = Enum.Material.Metal
	bullet.Size = Vector3.new(.2,.2,.2)
	bullet.Parent = script.Parent
	local barrel = tool.Barrel
	local barrel2 = barrel.Position
	local newcframe = CFrame.new(barrel2, hit.p)
	bullet.CFrame = newcframe
	
	local Velocity = Instance.new("BodyVelocity")
	Velocity.maxForce = Vector3.new(math.huge, math.huge, math.huge) 
	Velocity.Velocity = bullet.CFrame.LookVector * 100


	Velocity.Parent = bullet
	

	
	bullet.Touched:connect(function(hit) 
		if hit.Parent:FindFirstChild('Humanoid') and hit.Parent ~= player.Character  then
			local humanoid1 = hit.Parent:FindFirstChild('Humanoid')
			humanoid1:TakeDamage(20)

			bullet:Destroy()


		end
	end)
	bullet.Touched:connect(function(hit) 
		if hit:IsA("Part")  and hit.Parent ~= player.Character then

	bullet:Destroy()

		end

	end)
	
	
end



event.OnServerEvent:Connect(onEvent)

try printing the hit.Position to see if it changes when you move your mouse around, it could be an issue with transferring arguments

1 Like

What value does the “hit” parameter represent?

Mouse.hit


I added a print statement and its only printing 1 position regardless of where i point the mouse.

Then it is definitely a problem with your transferring of values, make sure your local script is sending the right data

1 Like

Ye. Is it possible for me to position the bullet into the barrel and use lookat to make it look at the mouse.

yeah, once you make sure the local script is firing the right arguments (speaking of I would make the local script send the hit.Position and rename it something in the arguments so that the touched event doesn’t interfere. After you’ve done all that use

bullet.CFrame = CFrame.new(barrel.Position, mousePos)
1 Like

Ye im stumped. I’m pretty sure im sending over the right info. It just keeps giving me the same location over and over again.

I tried the look at thing I mentioned but it didn’t work ):

can you show me the local script for when you click?

1 Like
local tool = script.Parent.Parent
local event = game.ReplicatedStorage.GunEvents.Bullet
local event2 = game.ReplicatedStorage.GunEvents.Reload
local shotsound = script.Parent.Shot
local canshoot = true
local cooldown = script.Parent.Cooldown.Value
local player= game.Players.LocalPlayer

local ammo = script.Parent.Ammo.Value

local mouse = player:GetMouse()
local hit = mouse.Hit.Position

function onShot()
	if ammo >= 1 then

		if canshoot == true then
			canshoot = false
			event:FireServer(shotsound, hit)
			script.Parent.MuzzleFlash.Enabled = true
			wait(cooldown)
			script.Parent.MuzzleFlash.Enabled = false
			canshoot = true
			ammo = ammo - 1
		end


	else
		print("Reloading")
		local char = player.Character or player.CharacterAdded:Wait()
		local humanoid = char.Humanoid
		local reload = script.Parent.Reload
		local animator = humanoid:FindFirstChildOfClass("Animator")
		local animationTrack = animator:LoadAnimation(reload)
		wait(.3)
		animationTrack:Play()
		wait(1)
		ammo = 15

	end


	end



tool.Activated:Connect(onShot)

local script is inside the tool

You’ve declared hit outside of the onShot() function. This might be why it’s always the same position.

1 Like

You’re sending the variable Hit which you’ve defined at the top. You have to define hit in the shot script, the reason its sending the same position over and over is because hit is never updated. It’s sending where your mouse was when you spawned.

1 Like

@hotpocket285 @Ferghins Thank you. I should have noticed that lol. It works now though