I have this raycasting script that casts a ray from one part to another, but it gives me the error attempt to perform arithmetic (sub) on nil

Here is my code

Client side

function CastRay() --//From the client
	local spring = require(replicatedStorage.FrameWorkObjects.SpringModules.spring)
	
	
	local unit1 = viewer.Unit1
	local unit2 = viewer.Unit2
	
	local rayOrigin = unit1.Position
	local rayDestination = unit2.Position
	
	RemoteEventsFolder.FireEvent:FireServer(viewer.Unit1.Position, mouse.Hit.Position)
end

Here's the server side

print("Ray script ready")
game.ReplicatedStorage.FrameWorkObjects.RemoteEvents.FireEvent.OnServerEvent:Connect(function(Player, FromP, ToP)
	local RayCast = Ray.new(FromP,(ToP-FromP).unit*100) --//Error starts here
	local Part,Position,Normal = game.Workspace:FindPartOnRay(RayCast,Player.Character, false, true)
	local Dist = (ToP-FromP).magnitude
	
	if not Dist then Dist = 300 end
	
	
	if Part and Part.Parent:FindFirstChild("Humanoid") and Part.Parent.Name ~= "Makarov" then
		Part.Parent.Humanoid:TakeDamage(40)
		local bloodParticle = game.ReplicatedStorage.BloodPart:Clone()
		
		bloodParticle.Position = Position
		bloodParticle.Parent = workspace
		
		game.Debris:AddItem(bloodParticle, 0.5)
	elseif Part and Part.Name == "Glass" then
		local shards = game.ReplicatedStorage.GlassShards:Clone()
		shards.Parent = workspace
		shards:SetPrimaryPartCFrame(Part.CFrame + Vector3.new(0, 3, 0))
		game.Debris:AddItem(shards, 10)
		Part:Destroy()
 	end
end)

Any help would be great!

3 comments

0

Print out all the parameters that were passed inside of the server. Check to see if one is equal to nil.

0

Okay

0

None of them are nil