Issues using CurrentCamera:ScreenPointToRay() To Make Throwable Tool Mobile-Friendly?

Thanks to some helpful devforum members, I managed to understand that I had to implement CurrentCamera:ScreenPointToRay() to convert where the player tapped on screen to a real Vector3 coordinate. However, seeing as I haven’t used this before, I’m experiencing errors.

For Example:
When Like This:

local ScreenToWorld = workspace.CurrentCamera:ScreenPointToRay(InputObjectPos)

I get an error stating that argument 2 is missing or nil, but when I add a number for parameter 2:

local ScreenToWorld = workspace.CurrentCamera:ScreenPointToRay(InputObjectPos, 50)

It errors: Argument 1 missing or nil.
I think my issue is my misunderstanding of how it works. All I want to do is make it so it fires the Vector3 equivalent of the touched position to the server script, so I can instance a ball. A bit of help showing me where I went wrong would be appreciated!

Full Local Script

local UIS = game:GetService("UserInputService")
local IsMobilePlayer = false
IsMobilePlayer = UIS.TouchEnabled -- Check if on mobile
local Event = game.ReplicatedStorage.FireSnowball
script.Parent.Equipped:Connect(function(Mouse)
	if IsMobilePlayer then
		UIS.TouchTap:Connect(function(InputObject)
			local InputObjectPos = InputObject.Position
			local ScreenToWorld = workspace.CurrentCamera:ScreenPointToRay(InputObjectPos, 50)
			Event:FireServer(game.Players.LocalPlayer, script.Parent.Handle.Position, ScreenToWorld)
		
		end)
	else
		local Mouse = game.Players.LocalPlayer:GetMouse()
		Mouse.Button1Down:Connect(function()
			Event:FireServer(game.Players.LocalPlayer, script.Parent.Handle.Position, Mouse.Hit.p)
		end)
	end
end)

Topic leading up to this

The documentation tells you how to create a ray with it.

ScreenPointToRay

1 Like