Getting raycasting error while doing everything right?

Alright, so for my game i’m working on making my guns mobile compatible. I made a custom UI to go with it.
The raycasting in my guns works fine when using mouse.Hit.Position, for example:

gun.Fire:FireServer(mouse.Hit.Position)

this will work fine, and my script will fire the bullet to the right mouse position.
For my mobile UI however, i just take the middle of the screen instead of a mouse Position and the script returns an error.

local Vector = Vector2.new(workspace.CurrentCamera.ViewportSize.X / 2, workspace.CurrentCamera.ViewportSize.Y / 2  - (game:GetService("GuiService"):GetGuiInset().Y/2))
gun.Fire:FireServer(Vector)	

The above code returns this error:
image

(I looked up how to get the middle of the screen on another devforum post, so I’m not sure if the calculation is accurate.)

The thing is, when i print the “MousePos” (or in this case the “Vector” variable from the code snippet) it prints out 2 numbers: 1 X, 1 Y and of course, no Z value, because it is a Vector2, not a Vector3.

Any tips?

Server side looks like this:

gun.Fire.OnServerEvent:Connect(function(player, mousePos)
	
	print(mousePos)
	local char = gun.Parent
	local humanoidChar = char:WaitForChild("Humanoid")
	
	if humanoidChar.Health ~= 0 then
		local raycastParams = RaycastParams.new()
		raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
		raycastParams.FilterDescendantsInstances = {player.Character, game.Workspace.SniperZoneModel}
		local handlepos = mainmodel.Position

		local raycastResult = workspace:Raycast(handlepos, (mousePos - handlepos)*300, raycastParams)
		if raycastResult then
			
			if AmmoVal.Value ~= 0 then
				firesound:Play()
				FX:Emit(1)
				enableLight()
				AmmoVal.Value = AmmoVal.Value - 1

				local SPREAD = 0.4
				local finalPos = raycastResult.Position + Vector3.new(math.random(-SPREAD, SPREAD), math.random(-SPREAD ,SPREAD), math.random(-SPREAD, SPREAD)) 

				if raycastResult.Instance.Parent:FindFirstChild("Humanoid") then
					raycastResult.Instance.Parent.Humanoid.Health = raycastResult.Instance.Parent.Humanoid.Health - Damage
					rs.VisualizeBullet:FireAllClients(att.WorldPosition, finalPos, true)
					HitsModule.AddHits(raycastResult.Instance.Parent, char.Name, Damage)
					EnemyHitEvent:FireClient(player)
				elseif raycastResult.Instance.Parent.Parent:FindFirstChild("Humanoid") then
					raycastResult.Instance.Parent.Parent.Humanoid.Health = raycastResult.Instance.Parent.Parent.Humanoid.Health - Damage
					rs.VisualizeBullet:FireAllClients(att.WorldPosition, finalPos, true)
					HitsModule.AddHits(raycastResult.Instance.Parent.Parent, char.Name, Damage)
				else
					rs.VisualizeBullet:FireAllClients(att.WorldPosition, finalPos, false)
				end

			else
				reloadsound:Play()
				task.wait(3)
				AmmoVal.Value = ClipSizeVal.Value
			end
		end

	end
end)

Thank you.

Edit: the error is at line 58, which is this line:

local raycastResult = workspace:Raycast(handlepos, (mousePos - handlepos)*300, raycastParams)

You should tell us which line gives the error, but i believe its this one:

You are subtracting a vector3 from vector2 and you cant do that.

I’m pretty sure (I might be wrong) that mousePos is a Vector3, like Mouse.Hit, instead of the actual position on the screen as a gui

mousePos is the parameter of the remote:

Which is a Vector2.

1 Like

This can’t be the issue though, because it does work when i use the mouse.hit.Position, which is also a Vector2.

mouse.Hit.Position gives you the position of the mouse in 3D space, meaning it does give you a Vector3.
https://developer.roblox.com/en-us/api-reference/property/Mouse/Hit

1 Like

You’re right! I’ve changed my script to now work with a Vector3 and that does the trick, however i’m still quite puzzled about the error. Why would it tell me to use a Vector2 when that obviously doesn’t work?

Whenever you are subtracting a Vector3 from Vector2 or other way around, it will always tell you that both need to be a Vector2. Honestly not sure why.

1 Like