Attempt to perform arithmetic (sub) on Vector3 and Instance (Ray casting)

Hey guys! How you doing ? I hope very very well! :grin: , Being direct, I made a Raycast system, but it doesn’t seems to work at 100% because of an error:

I haven’t seen this error in my experience as a developer, and I don’t really know how to fix this since I’ve done this, this way, and now it gives me this error.

I really appreciate your help and your person if you can give me a hand with this, even if it’s a small one. And with this said, here’s the explication:

  1. What do you want to achieve? Making a raycast system that goes from your right hand (rayOrigin) to your mouse position without any errors

  2. What is the issue?

  3. What solutions have you tried so far? Remaking new raycast scripts but they don’t work as well, so now I use the most well-made one of them.

What you need to know to understand the code is the following things:

  • 1 -) There is 3 scripts (1 LocalScripts and 2 ServerScript). The 1st one (ServerScript) creates and parents the “beam” effect to the player’s character. The 2nd (LocalScript) is just to get the mouse position and send the RemoteEvent to the Server. And the 3rd one (ServerScript) is just the handler of the RemoteEvent.

Here’s the LocalScript (the “mousePos” one)

wait(5)

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local mousePos 

local char = player.Character
local rayOrigin = char.RightHand.Position

player.CharacterAdded:Connect(function()

	--Give the Beam effects to the player 

	game.ReplicatedStorage:WaitForChild('RemoteEvents').GiveBeamToCharacter:FireServer(player)


end)


mouse.Move:Connect(function()
	
	mousePos = mouse.Hit.Position
	
	game.ReplicatedStorage:WaitForChild('VoteForSomeoneWithHands'):FireServer(player, rayOrigin, mousePos)
	
end)

-Here’s the 1st ServerScript (the “Beam effect giver” one) [This seems to work correctly]

game.ReplicatedStorage:WaitForChild('RemoteEvents').GiveBeamToCharacter.OnServerEvent:Connect(function(player)
	
	local BeamPartGuide = Instance.new('Part')
	BeamPartGuide.Name = "BeamPartGuide"
	BeamPartGuide.CanCollide = false
	BeamPartGuide.Anchored = true
	BeamPartGuide.Transparency = 0.9
	BeamPartGuide.Parent = player.Character
	
	local AlignPosition = Instance.new('AlignPosition')
	AlignPosition.Parent = player.Character:FindFirstChild('RightHand')
	AlignPosition.Name = "AlignPosition"


	local Attachment0 = Instance.new('Attachment')
	Attachment0.Parent = player.Character:FindFirstChild('RightHand')
	Attachment0.Name = "Attachment0"


	local BeamEndAttachment = Instance.new('Attachment')
	BeamEndAttachment.Parent = player.Character:WaitForChild('RightHand')
	BeamEndAttachment.Name = "BeamEndAttachment"


	local Beam = Instance.new('Beam')
	Beam.Parent = player.Character:FindFirstChild('RightHand')
	Beam.Name = "Beam"
	Beam.Attachment0 = Attachment0
	Beam.Attachment1 = BeamEndAttachment
	
	
end)



-The “mousePos RemoteEvent” handler (ServerScript)[Here’s where it shows the error]

local function MakeVisibleRayWithBeam(rayOrigin, RayDirection, char)

	local midPoint = rayOrigin + RayDirection/2

	--Block Part(BeamGuide) of the code

	local BeamPartGuide = char:FindFirstChild('BeamPartGuide')
	BeamPartGuide.Anchored = true
	BeamPartGuide.CFrame = CFrame.new(midPoint, rayOrigin)
	BeamPartGuide.Size = Vector3.new(0.1, 0.1, RayDirection.Magnitude)

	--Beam part of the code

	char:FindFirstChild('Beam').Enabled = true
	char:FindFirstChild('Beam').Size = BeamPartGuide.Size
	char:FindFirstChild('Beam').Position = BeamPartGuide.Position
	--char:FindFirstChild('BeamEndAttachment').Position = char.BeamPartGuide.Position


end



game.ReplicatedStorage:WaitForChild('VoteForSomeoneWithHands').OnServerEvent:Connect(function(player, rayOrigin, mousePos)	
	local char = player.Character
	
	local RayCastParameters = RaycastParams.new()
	RayCastParameters.FilterDescendantsInstances = player.Character:GetChildren() --Here this includes the BeamPart and the BeamPartGuide (Because they are inside the player's character)
	RayCastParameters.FilterType = Enum.RaycastFilterType.Blacklist
	
	local Range = 100 --Distance of how long can it go
	
	
	local RayDirection = (mousePos - rayOrigin).Unit * Range --Range --Here's where the "arithmetic error" is produced
	
	-- Cast the ray
	
	local raycastResult = workspace:Raycast(rayOrigin, RayDirection, RayCastParameters)	

	MakeVisibleRayWithBeam(rayOrigin, RayDirection, char)
	
	if not raycastResult then
		
		raycastResult = {Position = RayDirection}
		
	end

end)


You don’t need to pass the LocalPlayer when you call FireServer, it’s passed to the server automatically as the first arg

2 Likes

The error means that you are trying to substract a vector value from an instance object value
The issue is caused by this line in your localscript

You dont need to send a player object when you use :FireServer(), it includes one by its own as the first argument, just delete the player argument inside the function()

So basically in this line:

your player argument is the player, but since youre sending ANOTHER player object then your rayOrigin value is also becoming the player.

1 Like

Thanks, now this error doesn’t show up and I’m really grateful for it, thanks to your help, but there’s a problem with the CFrame with the BeamPartGuide, it says that it is nil for some reason:

image

Here’s the Script (It’s the same as the Remote Event Handler one)

local function MakeVisibleRayWithBeam(rayOrigin, RayDirection, char)

	local midPoint = rayOrigin + RayDirection/2

	--Block Part(BeamGuide) of the code

	local BeamPartGuide = char:FindFirstChild('BeamPartGuide')
	BeamPartGuide.CFrame = CFrame.new(midPoint, rayOrigin)
	BeamPartGuide.Size = Vector3.new(0.1, 0.1, RayDirection.Magnitude)
	
	--Beam part of the code

	char:FindFirstChild('Beam').Enabled = true
	char:FindFirstChild('Beam').Position = BeamPartGuide.Position


	
	return BeamPartGuide
end