Bullet travel problem

Alright so I’m working on a gun system, learning about creating one myself, I made an actual system where the bullet travels, but I also made a module script with gun settings I set up…

So my problem is when full auto is false the bullet spawns in front of the gun and does it’s thing, but when it’s on full auto the bullet spawns in like couple feet away from the gun, I even tried an auto clicker and that worked fine, maybe it’s the function that I misdone?

Non-auto:

(Bullet starts from gun)

Full-auto:

(Even if I use auto clicker when it’s not full auto and no firerate it still starts from the gun, but when full auto is enabled it does not)

Click functions:

Non-auto:

	tool.Activated:Connect(function()

		if gunsettings.auto == true then return end

		onfire(mouse)

	end)

Full-auto:

	mouse.Button1Down:Connect(function()

		if gunsettings.auto == false then return end

		mousedown = true

		onfire(mouse)

	end)

Onfire:

function onfire(mouse)

	if auto == true then

		while mousedown == true do

			if isshooting == false then

				isshooting = true

				script.Parent.Handle.Fire:Play()

				createray(mouse)

				wait(firerate)

				isshooting = false

			end

			wait()

		end

	elseif auto == false then

		if isshooting == false then

			isshooting = true

			script.Parent.Handle.Fire:Play()

			createray(mouse)

			wait(firerate)

			isshooting = false

		end

	end

end

If you need raycast code reply down below

Its defiantly coming out the front since if you look at it there’s a slight white line from where it spawns. To me it just seems for some reason why it doesn’t look like it is since maybe the bullet is going out to fast…

1 Like

No when the speed is slower, the bullet still spawns couple feet away on full auto

There are many reasons that might cause this, we can’t tell for sure.
Check out maybe the bullet is CanCollide and Collides with the reset of the bullets.
Also, it would be helpful to send your code so we can understand the source of the issue.

1 Like
function createray(mouse)

	local accurateray = Ray.new(tool.Barrel.CFrame.p, (mouse.Hit.p - tool.Barrel.CFrame.p).unit * 300)
	local ray = Ray.new(accurateray.Origin, CFrame.Angles(math.rad(math.random(-spread, spread)), math.rad(math.random(-spread, spread)), math.rad(math.random(-spread, spread))) * accurateray.Direction)

	local hit, position = game.Workspace:FindPartOnRay(ray, user)

	local distance = (position - tool.Barrel.CFrame.p).magnitude
	local rayPart = Instance.new("Part", user)
	rayPart.Name = "RayPart"
	rayPart.BrickColor = BrickColor.new("White")
	rayPart.Material = "SmoothPlastic"
	rayPart.Transparency = 0.92
	rayPart.Anchored = true
	rayPart.CanCollide = false
	rayPart.TopSurface = Enum.SurfaceType.Smooth
	rayPart.BottomSurface = Enum.SurfaceType.Smooth
	rayPart.formFactor = Enum.FormFactor.Custom
	rayPart.Size = Vector3.new(0.2, 0.2, distance)
	rayPart.CFrame = CFrame.new(position, tool.Barrel.CFrame.p) * CFrame.new(0, 0, -distance/2)

	local bullet = game.ReplicatedStorage.GunStuff.Bullets:FindFirstChild("SimpleBullet"):Clone()
	bullet.Parent = game.Workspace
	bullet.CFrame = tool.Barrel.CFrame
	bullet.CFrame = CFrame.new(bullet.Position, position)

	local bodyv = Instance.new("BodyVelocity", bullet)
	bodyv.Velocity = bullet.CFrame.LookVector * bulletspeed
	bodyv.MaxForce = Vector3.new(math.huge, math.huge, math.huge)

	bullet.Touched:Connect(function(hitt)

		if hitt.Name == "RayPart" or hitt.Name == "Barrel" or hitt.Name == "GunPart" then

		else

			bullet.Anchored = true
			bullet.Transparency = 1
			bullet.Sound:Play()

			local bullet2 = Instance.new('Part', game.Workspace)
			--bullet.FormFactor = Enum.FormFactor.Custom
			bullet2.Size = Vector3.new(0.1, 0.1, 0.1)
			bullet2.BrickColor = BrickColor.new("Black")
			bullet2.Shape = Enum.PartType.Block
			bullet2.CanCollide = false
			bullet2.CFrame = CFrame.new(position)
			bullet2.Anchored = true
			bullet2.TopSurface = Enum.SurfaceType.Smooth
			bullet2.BottomSurface = Enum.SurfaceType.Smooth
			bullet2.Name = 'Bullet2'

			game.Debris:AddItem(bullet2, 0.5)

		end

	end)

	spawn(function()
		for i = 1, 8 do
			rayPart.Transparency = rayPart.Transparency + 0.01
			wait()
		end
	end)

	game.Debris:AddItem(bullet, gunsettings.range)
	game.Debris:AddItem(rayPart, 0.4) -- 0.02

end

but do you see what I mean? If you still look at the muzzle there’s still that slight white line from the bullet.

1 Like

That’s the trial, if I remove the trial the bullet will almost be unseen

I want to assume it is an issue related to the bullet being incredibly fast, but as you showed it only happens once Auto is on. Are you calling the same functions in both Auto and non-Auto?

1 Like

The ray are exactly the same, even if I shoot faster than full auto with auto clicker the bullet is perfectly fine

I don’t really know why this is happening, try to debug your code.

1 Like

What do you mean, like check everything?

I suggest putting prints in every point of the script, see what’s printing out properly and what’s not. Your code seems fine and the issue is weird.

1 Like

Sure, I’ll do that later as I have to eat dinner right now, see you then

1 Like

I debugged it and it works just as fine, I really don’t know what to do for this one I guess

image

The source of the issue is unclear, I am unable to think of a possible reason for that to happen.

1 Like

Same thing over here, I would probably have to rescript it in a different way

I guess so. Roblox sometimes can be very frustrating.

1 Like

The framerates drop when you fire your gun resulting in the bullets being initially rendered. You can try to fix this by adding a delay.

1 Like

that’s not the case, I’ve shot with an auto clicker which is wayy faster than full auto, once full auto is enabled the bullet spawns further than the barrel