Delayed Bullet trail

  1. What do you want to achieve? Keep it simple and clear!
    I’m trying to create a bullet trail effect for a pistol I am making but its very delayed and I’m trying to figure out how to speed it up.

  2. What is the issue? Include screenshots / videos if possible!
    I’ve gotten the effect to work so far, but the problem is its very delayed(Look at media I have included)
    Loud Audio Warning



As you can see when I’m standing still the effect looks perfectly fine but when I move it’s very delayed.

  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I’ve tried shortening the code to make it faster but the delayed effect persisted I’ve tried looking for people with similar issues but came up with nothing helpful.

Main code–


function particle(part1, part2, result)
	
	local magnitude = math.abs((part1.Position - part2.Position).Magnitude)

	local trailpart = script.Parent.PremadePart:Clone()

	trailpart.Parent = workspace

	trailpart.CFrame = CFrame.lookAt(part1.Position, part2.Position)

	local pos = (part1.Position + part2.Position)/2

	trailpart.Position = pos

	trailpart.Size = Vector3.new(trailpart.Size.X, trailpart.Size.Y, magnitude)

end

This main code takes in 1. The part the effect will come from(the tip of the gun) part 2. The part it will hit and the result is the result of a raycast that the gun uses to determine what it hit. The whole goal of the code is to take the effect part and to resize its position and orientation to create the shooting effect.
This is located in the server script that is mentioned later


tool.Equipped:Connect(function()
	local char = tool.Parent
	local plr = game.Players:GetPlayerFromCharacter(char)
	local mouse = plr:GetMouse()
	local idle_animation_track = tool.Parent.Humanoid:LoadAnimation(tool.Idle)
	idle_animation_track:Play()
	tool.Activated:Connect(function()
		if debounce == true and tool.Ammo.Value > 0 and reloading == false then
			debounce = false
			remote_event:FireServer(mouse.Hit.Position)
			local shoot_animation_track = tool.Parent.Humanoid:LoadAnimation(tool.Shoot)
			shoot_animation_track:Play()
			wait(.25)
			debounce = true
		else
			local value = "other"
			tool.Reload:FireServer(value)
			end
		end)

-excerpt from a local script where the tool is activate and where the position the mouse clicked on is passed to a server script via a remote event.


remote_event.OnServerEvent:Connect(function(plr, mouse_pos)
	local origin = tool.ShootPart.Position
	local direction = mouse_pos - origin
	local raycastParams = RaycastParams.new()
	raycastParams.FilterDescendantsInstances = {tool.Parent, tool.Parent:GetChildren(), tool.Handle, tool.ShootPart}
	raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
	raycastParams.IgnoreWater = true
	local ray_result = workspace:Raycast(origin, direction * 10, raycastParams)
	particle(script.Parent.ShootPart, ray_result, ray_result)

-excerpt from the server script that receives the mouse position and creates the raycast.
You can see the particle effect being created by the function in the last line, passing along those 3 variables.

From what I see, its likely the Server thats causing this Delay, or the Raycast itself, since this is a small Detail, you should Handle it on the Client Instead of the Server, to avoid Latency issues.

That isnt a very good idea.

If you are planning to Add Range to the Weapon, make sure to Normlize the Displacement using .Unit like how you would with .Magnitude

This Enum has been Deprecated, use Enum.RaycastFilterType.Exclude Instead.

Its RaycastResult.Instance if you want to get the second part.

The Pythagorean Theorem will always return as a Positive Value, because the numbers are made positive in order or it work when finding the square root of d

I’d recommend you to just use the “Fast Cast” module

That’s harder to use rather than basic raycasting; the FastCast module is preferably used only when the developer wants to add projectiles and bullet drop.

yeah, but that looks way better, anyways it’s up to the creator, he’ll choose whatever he wants :man_shrugging:

I have kinda temporary solution, worked for me.
Just add players velocity to ray origin.

local origin = tool.ShootPart.Position + plr.Character.HumanoidRootPart.AssemblyLinearVelocity