Ray Cast returning inconsistency

In my game I run the RayCast on client:

local RayCast = Ray.new(Tool.B.Position, (TargetCFrame.p - Tool.B.Position).unit * Range)
local Part, Position = workspace:FindPartOnRay(RayCast, Player.Character, false, true)
print(Part)
								
GunEvent:FireServer(RayCast, Part, Position, TargetCFrame)

This works fine and Part prints to a part that the ray hits

But the problem is when I go onto the server:

function OnFire(Player, RayCast, Part, Position, TargetCFrame)
print(Part)

Sometimes Part will print nil on the server even though the part printed locally, and there are no parts that are created locally in my game so it’s not like I’m hitting some part only I can see

Any response is appreciated, thanks

1 Like

Is there any way you can show us more of your scripts? Maybe there is something else going on here that we can’t see.

Sure:

LocalScript:

function OnButtonDown()
	if Player.Character then
		if Tool.Parent == Player.Character then
			MouseDown = true
			while true do
				if MouseDown == true then
					if Ammo > 0 then
						if CanShoot == true then
							if Reloading == false then
								Shooting = true
								CanShoot = false
								
								Ammo = Ammo - 1
								TextLabel.Text = Ammo.."/"..MaxAmmo
								local TargetCFrame = Mouse.Hit
								
								ShootTrack:Play()
								
								local RayCast = Ray.new(Tool.B.Position, (TargetCFrame.p - Tool.B.Position).unit * Range)
								local Part, Position = workspace:FindPartOnRay(RayCast, Player.Character, false, true)
								
								GunEvent:FireServer(RayCast, Part, Position, TargetCFrame)
								
								local FOVThread = coroutine.create(FOV)
								coroutine.resume(FOVThread)
								
								wait(ShotDelay)
								CanShoot = true
							else
								Shooting = false
								break
							end
						else
							Shooting = false
							break
						end
					else
						--No Ammo
						Shooting = false
						Reload()
						break
					end
				else
					Shooting = false
					break
				end
			end
		end
	end
end

Mouse.Button1Down:Connect(OnButtonDown)

ServerScript:

function OnFire(Player, RayCast, Part, Position, TargetCFrame)
	if Player.Character:FindFirstChild(Tool.Name) then
		local Position = TargetCFrame.p
		
		local MuzzleFlashThread = coroutine.create(MuzzleFlash)
		coroutine.resume(MuzzleFlashThread)
		
		local SFXThread = coroutine.create(GunShotSFX)
		coroutine.resume(SFXThread)
		
		if not Part then print("No part found!") end
		
		if Part then
			print(1)
			ProjectileVisualization:FireAllClients(Tool, BulletColor, TargetCFrame, Part)
			local Hit = Part
			if Hit.Parent ~= Tool then
				print(2)
				local TargetCharacter = FindCharacter(Hit)
				if TargetCharacter then
					print(3)
					local TargetHumanoid = TargetCharacter:FindFirstChild("Humanoid")
					if TargetHumanoid then
						print(4)
						if TargetHumanoid.Health > 0 then
							print(5)
							--It has a Humanoid!
							--Check if it's a player
							local TargetPlayer = Players:FindFirstChild(TargetHumanoid.Parent.Name)
							if TargetPlayer ~= Player then
								if TargetPlayer then
									--It's a player!
									if Player.Team then
										if TeamKill == false then
											if Player.Team ~= TargetPlayer.Team then
												local HeadshotBonus = 0
												if Part.Name == "Head" then
													HeadshotBonus = Tool.Code.Values.HeadshotBonus.Value
												end
												local Damage = math.random(MinimumDamage.Value, MaximumDamage.Value) + HeadshotBonus
												if TargetHumanoid.Health - Damage <= 0 then
													--Killed a player
													--Player.leaderstats.Kills.Value = Player.leaderstats.Kills.Value + 1
												end
												TargetHumanoid:TakeDamage(Damage)
											end
										end
									else
										local HeadshotBonus = 0
										if Part.Name == "Head" then
											HeadshotBonus = Tool.Code.Values.HeadshotBonus.Value
										end
										local Damage = math.random(MinimumDamage.Value, MaximumDamage.Value) + HeadshotBonus
										if TargetHumanoid.Health - Damage <= 0 then
											--Killed a player
											--Player.leaderstats.Kills.Value = Player.leaderstats.Kills.Value + 1
										end
										TargetHumanoid:TakeDamage(Damage)
									end
								else
									--NPC!
									local HeadshotBonus = 0
									if Part.Name == "Head" then
										HeadshotBonus = Tool.Code.Values.HeadshotBonus.Value
									end
									local Damage = math.random(MinimumDamage.Value, MaximumDamage.Value) + HeadshotBonus
									if TargetHumanoid.Health - Damage <= 0 then
										--Killed a NPC
										--Player.leaderstats.Kills.Value = Player.leaderstats.Kills.Value + 1
									end
									TargetHumanoid:TakeDamage(Damage)
								end
							end
						end
					end
				end
			end
		end
	end
end

GunEvent.OnServerEvent:Connect(OnFire)
2 Likes

You should try getting the part on ray in the server script instead of the local script and see how that goes. I’m on my phone right now so I can’t try it myself, but that may solve the issue. Let me know if that works!

Edit: You can fire the raycast and the CFrame through the event, and then on the server just do what you did in the local script.

Yeah I was doing that earlier but it’s very laggy and the gameplay is pretty bad because of it

1 Like

I’m not too strong with Rays but I think you should create the Ray server sided instead of client. The client should just get the users input so in this case the Tool.B.Position and the target frame. Send those through the event, then create the ray there and get the part on hit. You can probably setup another function for that. That’s the best I could think of, I can’t really see why the part wouldn’t be going to the server when the part is replicating to both the client and server. I’d try and create the ray through the script. Other than that, I’m intrigued on what the issue could be.

1 Like

That’s what I did before but since it’s scanning for parts on the server there’s a delay so if you shoot someone while they are moving it would lag and not register the shot, you would have to aim where they are moving towards, creating the ray locally was the only way I could do it severe performance drop.

Found the issue it was a stray bullet running in the way :face_palm:

2 Likes

If you’ve found the solution please mark your post as the solution.

1 Like