I am trying to make a gun script but when I fire the rayevent and the server side script tries to get the current part on ray I get the error saying that the part is nil.

Server Side Script


local TweenService = game:GetService("TweenService")

local DetectRayHit = game:GetService("ReplicatedStorage"):WaitForChild("RemoteEvents").DetectRayHit

DetectRayHit.OnServerEvent:Connect(function(player, ray, startPos, orientation, BulletSpeed)						
																						
		local part,position = workspace:FindPartOnRayWithIgnoreList(ray,{player.Character,workspace.InvisibleWallFront})
		
		print(part.Name)
		
		if part ~= nil then
			
			local beam = Instance.new("Part", workspace)
			beam.BrickColor = BrickColor.new("Bright red")
			beam.FormFactor = "Custom"
			beam.Material = "Neon"
			beam.Transparency = 0.25
			beam.Anchored = true
			beam.Locked = true
			beam.CanCollide = false
			beam.Position = startPos
			beam.Orientation = orientation
			beam.Size = Vector3.new(.2,.2,2)	
			
			local goal = {
				Position = position
			}
			
			print("bullet speed is "..BulletSpeed)

			local tweenInfo = TweenInfo.new((beam.Position - position).Magnitude * 1 / 20, Enum.EasingStyle.Linear, Enum.EasingDirection.In, 0,false,0)
			
			local Tween = TweenService:Create(beam,tweenInfo,goal)
			
			Tween:Play()

		end
		
end)

Client Side Script

local tool = script.Parent

local UserInputService = game:GetService("UserInputService")

local OnCastEvent = game:GetService("ReplicatedStorage"):WaitForChild("RemoteEvents").DetectRayHit

local player = game:GetService("Players").LocalPlayer

local mouse = player:GetMouse()

local Delay = tool:WaitForChild("Delay")
local OverheatDelay = tool:WaitForChild("OverheatDelay")

local OverheatLimit = tool:WaitForChild("OverheatLimit")

local BulletSpeed = tool:WaitForChild("BulletSpeed")

local TimesFired  = 0

local debounce = false

local BlastSoundEffect = tool:WaitForChild("BlastSoundEffect")

local OverheatUI = player:WaitForChild("PlayerGui"):WaitForChild("GunOverheatUI")

local reloading = false

function shootLaser()
	local ray = Ray.new(tool.Point.CFrame.p,(mouse.Hit.p - tool.Point.CFrame.p) * math.huge)	
	OnCastEvent:FireServer(ray,tool.Point.Position,tool.Point.Orientation,BulletSpeed.Value)
end

UserInputService.InputBegan:Connect(function(key, gameProccessedEvent)
	if gameProccessedEvent == true then
		if key.KeyCode == Enum.KeyCode.R then
			reloading = true
			print("reloading")
			OverheatUI.Main.Overheat.BackgroundColor3 = Color3.new(255,0,0)
			wait(OverheatDelay.Value)
			OverheatUI.Main.Overheat.Size = UDim2.new(.024,0,1,0)
			OverheatUI.Main.Overheat.BackgroundColor3 = Color3.new(63, 255, 23)
			TimesFired = 0
			reloading = false 
		end
	end
end)



tool.Equipped:Connect(function()
	OverheatUI.Main:TweenPosition(UDim2.new(0.007, 0,0.883,0))
	mouse.Icon = "rbxassetid://4812787581"
end)

tool.Unequipped:Connect(function()
	OverheatUI.Main:TweenPosition(UDim2.new(0.007, 0,1,0))
	mouse.Icon = ""
end)

tool.Activated:Connect(function()
	if debounce == false and reloading == false then
		debounce = true
		shootLaser()
		BlastSoundEffect:Play()
		TimesFired = TimesFired + 1
		OverheatUI.Main.Overheat.Size = UDim2.new((TimesFired/10),0,1,0)
		wait(Delay.Value)
		if TimesFired == OverheatLimit.Value then
			print("overheated")
			OverheatUI.Main.Overheat.BackgroundColor3 = Color3.new(255,0,0)
			wait(OverheatDelay.Value)
			OverheatUI.Main.Overheat.Size = UDim2.new(.024,0,1,0)
			OverheatUI.Main.Overheat.BackgroundColor3 = Color3.new(63, 255, 23)
			TimesFired = 0
		end
		debounce = false
	end
end)