Help with adding bullet to my gun

So I am making a gun for my game, and it works perfectly except it has no bullet trail. I have made ray casts visible with parts, but I can’t figure out how to add that to my code.

Here is the code:

local radius = 0.1
script.Parent.Fire.OnServerEvent:Connect(function(player, mousePos)
	local raycastParams = RaycastParams.new()
	raycastParams.FilterDescendantsInstances = {player.Character}
	raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
	
	local raycastResult = workspace:Raycast(script.Parent.RayPlace.Position, (mousePos - script.Parent.RayPlace.Position) * script.Parent:GetAttribute("BulletRange"),raycastParams)
	
			if raycastResult then
		
			local hitPart = raycastResult.Instance
			local model = hitPart:FindFirstAncestorOfClass("Model")

			if model then
			if model:FindFirstChild("Humanoid") then
				model.Humanoid.Health -= script.Parent:GetAttribute("Damage")
				end
		end
	end
	
end)

It’s a server script inside the tool.

Here is the code for the visible ray cast I want to add:

local radius = 0.1
local function visRay(ray)
	local midpoint = ray.Origin + ray.Direction/2
	
	local part = Instance.new("Part")
	part.Parent = game.Workspace
	part.Anchored = true
	part.CFrame = CFrame.new(midpoint, ray.Origin)
	part.Size = Vector3.new(radius, radius, ray.Direction.magnitude)
	part.Color = Color3.fromRGB(255, 255, 0)
	part.Material = "Neon"
end

local part = script.Parent

local origin = part.Position
local direction = part.CFrame.LookVector * 10

local ray = Ray.new(origin, direction)

visRay(ray)

If there are any better ways, please say them!

You could just add a trail or like a particle to the bullet and then when it gets shot and adds the bullet to debris it’ll disappear with it

There is no bullet, it’s just an invisible ray cast. I just realized how the title is miss leading, I’ll change it.

Look at this for reference if this is what you’re getting at?

Yeah basically, I wanted to make it just a yellow part that would appear for about 0.25 seconds like in zombie rush. I guns have a more classic style like zombie rush not like in phantom forces. The solution to that topic was making a part and scale it to the mouse position, any idea on how I can do that?

I think what you could do, is use TweenService to tween the part from the start to the end position & change it’s sizes? Probably something along the lines like this?

Yeah exactly what I was looking for. The problem is I am not sure how I can do something like that. All I need to figure out is how to generate a part that lines up with the ray cast for the gun so it accurately shows where the bullet is. There is also no bullet travel, the ray cast just appear, I don’t know how to add bullet travel, so tweening the part from start to finish wouldn’t really work.

EDIT: I got it to spawn a part whenever you click, just need to adjust the size and placement.

This could be a really helpful resource for you :stuck_out_tongue:

It doesn’t really help that much because I have almost everything I need, I just need the bullet to start at the tip of the barrel, it just spawns at where the tip of the barrel was when I created the model in the workspace, not at the tip of the barrel when the play holds it. here is the mod that I made for the script 10 minutes ago:

local radius = 0.1
script.Parent.Fire.OnServerEvent:Connect(function(player, mousePos)
	local raycastParams = RaycastParams.new()
	raycastParams.FilterDescendantsInstances = {player.Character}
	raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
	
	local trail = Instance.new("Part")
	trail.CFrame = CFrame.new(script.Parent.RayPlace.Position, mousePos)
	trail.Size = Vector3.new(radius,radius,script.Parent:GetAttribute("BulletRange"))
	trail.Material = Enum.Material.Neon
	trail.Color = Color3.fromRGB(255, 255, 0)
	trail.Anchored = true
	trail.CanCollide = false
	trail.Parent = game.Workspace
	wait(0.25)
	trail:Destroy()
	
	local raycastResult = workspace:Raycast(script.Parent.RayPlace.Position, (mousePos - script.Parent.RayPlace.Position) * script.Parent:GetAttribute("BulletRange"),raycastParams)
	
			if raycastResult then
		
			local hitPart = raycastResult.Instance
			local model = hitPart:FindFirstAncestorOfClass("Model")

			if model then
			if model:FindFirstChild("Humanoid") then
				model.Humanoid.Health -= script.Parent:GetAttribute("Damage")
				end
		end
	end
	
end)

Any idea on how I could do this? I didn’t really notice anything that helped with the part that I am stuck on.

I made a different gun using the exact same scripts and names, and it didn’t work. The same problem always happened. The bullet would spawn at 0,0,0, not where I actually needed it to spawn. Sorry for replying late, I was busy.

Have you tried using the logic in the tutorial?

bullet_clone.CFrame = CFrame.new(origin, intersection)*CFrame.new(0, 0, -distance/2)

Yeah I tried that, but It still didn’t work at all. I literally made a gun using the exact same scripts and the same way of making the bullet, but it still didn’t work, the same problem happened.