Raycasts aren't accurate if the player is moving

I am trying to make an fps game, and I am using raycasting for the bullets, as from what I heard, it is a good method of hit registry.
However, while the raycasts do work and damages a player if both ends are standing still, as soon as any person moves, the raycasts become inaccurate and don’t hit, even if the person is being clicked on at point blank.
The start of the shot is in startercharacterscripts, while the script that calculates the raycast is on serverscriptservice.

-- starting script
local raycastOrigin = script.Parent:WaitForChild("RaycastOrigin")

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local rayFolder = game.Workspace.Rays

mouse.TargetFilter = rayFolder

local pistolShot = game.ReplicatedStorage:WaitForChild("PistolShot")
local shooting = false
local reload = game.ReplicatedStorage:WaitForChild("GlobalReloadCheck")

mouse.Button1Down:Connect(function()
	if game.ReplicatedStorage.ShotsLeft.Value > 0 then
		if reload.Value == false then
		if shooting == false then
			local mousePosition = mouse.Hit.Position
			local originPosition = raycastOrigin.Position
	
			pistolShot:FireServer(mousePosition, originPosition)
			shooting = true
			wait(.175)
			shooting = false
		end
		end
		end
end)
-- raycast script
local PistolEvent = game.ReplicatedStorage:WaitForChild("PistolShot")
local rayFolder = workspace.Rays
local debris = game:GetService("Debris")

local range = 5000

PistolEvent.OnServerEvent:Connect(function(player, mousePos, originPos)
	local direction = (mousePos - originPos).Unit * range
	
	local result = workspace:Raycast(originPos, direction)
	
	if result then
		local char = result.Instance.Parent
		local hum = char:FindFirstChildWhichIsA("Humanoid")
	
		if hum and hum ~= player.Character.Humanoid then
			
			local Head = char["Head"]
			local Torso = char["Torso"]
			local Larm = char["Left Arm"]
			local Rarm = char["Right Arm"]
			local Lleg = char["Left Leg"]
			local Rleg = char["Right Leg"]
			
			hum:TakeDamage(15)
			
			Head.BrickColor = BrickColor.new("Persimmon")
			Torso.BrickColor = BrickColor.new("Persimmon")
			Larm.BrickColor = BrickColor.new("Persimmon")
			Rarm.BrickColor = BrickColor.new("Persimmon")
			Lleg.BrickColor = BrickColor.new("Persimmon")
			Rleg.BrickColor = BrickColor.new("Persimmon")
			wait(.125)
			Head.BrickColor = BrickColor.new("Medium stone grey")
			Torso.BrickColor = BrickColor.new("Medium stone grey")
			Larm.BrickColor = BrickColor.new("Medium stone grey")
			Rarm.BrickColor = BrickColor.new("Medium stone grey")
			Lleg.BrickColor = BrickColor.new("Medium stone grey")
			Rleg.BrickColor = BrickColor.new("Medium stone grey")
		end
		
	end
	
end)

Thanks

4/2/22 - It turns out that it’s only when the target moves that the raycast becomes innacurate, if the player shooting moves, it’s fine.

Where is the origin value coming from? Is it changed with movement?

the origin value comes from a part attatched in front of the players head

Is the value of that parts position updated everytime you do the raycast?
Everytime the raycast code runs, you need to retrieve the new position of that part, because its moving.

I think that the main problem is that the hitbox of the player lags behind due to high ping issues. When doing a test in studio, it appears to work, however when I test in the actual game, the raycasts deal no damage.

That is indeed the problem because raycast is on the server. This is why most shooter games has the raycast on the client.

Thanks to the people who have replied, but I have decided to just use fastcast in order to determine the hit registry.