Raycasting problem with detecting hit?

I have created Raycast weapons for the game I am working on. It functions by the firing the server when the weapon is fired, though I have a major problem. For some reason the Raycast which is casted from the weapon is not detecting hits on a character properly, I am unsure why but have a suspicion that the raycasting is being delayed. Here is a gif of this:

https://gyazo.com/c5e625d299b752a54cb10e0721a64575

The raycasting code I use:

Remote.OnServerEvent:Connect(function(player, muzzlePos, mousePos, Weapon, Head, Damage, bulletSpread, Speed, BulletType)
	local Ignore = {player.Character}
	table.insert(Ignore, player.Character)
	local Spread = bulletSpread * 100
	local ray = Ray.new(Head, (mousePos - Head).unit * 4000)

	local Part, Position, Vector = game.Workspace:FindPartOnRayWithIgnoreList(ray, Ignore)
	local Distance = (muzzlePos - Position).magnitude
	
	if Part then
		if Part.Parent:FindFirstChild("Humanoid") then
			Part.Parent:FindFirstChild("Humanoid"):TakeDamage(Damage)
		elseif Part.Parent:FindFirstChild("Zombie") then
			Part.Parent:FindFirstChild("Zombie"):TakeDamage(Damage)
		elseif Part.Parent.Parent and Part.Parent.Parent:FindFirstChild("Humanoid") then
			Part.Parent.Parent:FindFirstChild("Humanoid"):TakeDamage(Damage)
		end
	end
	Remote:FireAllClients(player, Position, Distance, muzzlePos, Weapon, Head, Speed, BulletType, Part, Vector)
end)

Could anyone help me with this?

1 Like

Seem to be missing some of the variables in that raycast from what you sent but I’d recommend trying out the screen point to ray function. It uses raycasting to find the hit position and then you could draw the raycast between the player and that point to check from obstructions. Hope this helps!

2 Likes

Yep. That’s just a delay affected by your server-client connection aka. ping.
Scripting-wise there’s nothing wrong.

2 Likes

So I should be creating the ray on a local script? Wouldn’t this allow exploiters to cheat?

1 Like

You shouldn’t change anything as it actually does what it’s intended to do.

Delayed communication is an issue all games face. Though, games which rely on server-client communication it being projectiles, actions or whatnot have different ways of dealing with this issue.

I’m not the most experienced when it comes to lag compensation but I know the theoretical part behind it. A method a lot of games use is ‘rewinding’ the server back in time based on the client who fired the projectile’s ping. You can do so by ‘recording’ all the character positions in your server, ‘rewinding’ back to roughly when the client locally fired the projectile (roughly represented by the ping) and using this data to simulate where/what the projectile would hit at that time.

1 Like

The easy way out is tracking all the raycast hits locally and then doing distance checks on the server. It’s no esports solution but it works.

Could you give me a method of how I would do a distance check? I am new to ray cast weapons.

Distance in ‘vector language’ is called magnitude. To get the magnitude between two vector positions, you would simply subtract them and read the new vector’s magnitude like so: (p1 - p2).magnitude.

However, I don’t agree with this being a reliable method to counter improper information sent by the client (via exploiting) as magnitude has nothing to do with directions, meaning you could simply tell the server to shoot the guy behind you as you’re looking the other way and the server would just allow it.

More accurate, but definitely less secure. You’d have to do some line of sight checks to be sure if you use this method.