Raycasting position issue

Basicly when you are not walking and you shoot the beam looks fine but when you walk and you shoot looks kinda broken.

this: image

I dont know how I can fix I tried to change the position but nothing.

Script:

local range = 400
local NormalDamage = 50
local HeadDamage = 65

local debounce = false

game.ReplicatedStorage.ToolsEvent.RayGunShootEvent.OnServerEvent:Connect(function(Player, TargetLocation, BulletLocation)
	if Player.Character == nil then
		return
	end

	local Beam = Instance.new("Part", game.Workspace)
	Beam.Color = Color3.fromRGB(0, 255, 0)
	Beam.Transparency = 0
	Beam.FormFactor = "Custom"
	Beam.Material = "Neon"
	Beam.Anchored = true
	Beam.CanCollide = false

	local distance = (BulletLocation.Position - TargetLocation).magnitude
	Beam.Size = Vector3.new(0.1, 0.1, distance)
	Beam.CFrame = CFrame.new(BulletLocation.Position, TargetLocation) * CFrame.new(0, 0, -distance/2)

	game.Debris:AddItem(Beam, 0.1)

	local NewRay = RaycastParams.new()
	local RayDirection = (TargetLocation - BulletLocation.Position) * range 

	NewRay.FilterDescendantsInstances = {Player.Character}

	local Result = game.Workspace:Raycast(BulletLocation.Position, RayDirection, NewRay)
	local humanoid =  Result.Instance.Parent:FindFirstChild("Humanoid")
	

	if Result then
		if Result.Instance then
			if not humanoid then
				return
			end
			if  Result.Instance.Name == "Head" then
				humanoid.Health -= HeadDamage
				
				local tag = Instance.new("ObjectValue")
				tag.Name = "creator"
				tag.Value = Player
				tag.Parent = humanoid
				
			elseif humanoid then
				humanoid.Health -= NormalDamage
				
				local tag = Instance.new("ObjectValue")
				tag.Name = "creator"
				tag.Value = Player
				tag.Parent = humanoid
				
			end
		end
	end
end)

This occurs due to network latency.

When the RemoteEvent is recieved on the Server’s end - the Beam is created at the position where the player called the event.
However, there’s a few milisecond delay between this - thus - on the player’s screen, they have moved forward a bit before the Server creates the beam, and thus it looks like the beam is behind.

You can fix this by moving the Beam creation code to the Client - but for replication purposes you would also have to FireAllClients to create the Beam as well.

You could also try offsetting the position of the Beam by the Player’s Velocity - but that might be equally wonky if your player is stopping and moving, or strafing. (Although, if you can find a way to define the player’s Ping - you could probably guess this more accurately).

As an alternate solution - you might be able to wait for the Beam to be created by the Server on the client - and then adjust its CFrame to line up correctly on the player’s screen - there might be graphical bugs with this but it’d be the simplest solution while retaining replication.

1 Like

ok but how I can fix that milisecond delay? If I change the position the beam will looks bad I will try the fist one

Is a good idea if I try to use beams with attachments?