How to know where the player tapped?

local function FireRay(Position)
	local UnitRay = Camera:ViewportPointToRay(Position.X, Position.Y)
	local ray = Ray.new(UnitRay.Origin, UnitRay.Direction * 100)

	local HitPart, WorldPosition = game.Workspace:FindPartOnRay(ray)

	return WorldPosition
end

This is the current function that I’m using, and it works, but it return the position a little higher than
where I clicked/tapped as you can see in the picture below

Please tell me what I’m doing wrong, and what can I do to fix it,
Thanks.

https://developer.roblox.com/en-us/api-reference/function/WorldRoot/FindPartOnRay
deprecated
maybe this helps?

I tried raycast but it’s still the same

How are you getting the player’s mouse position?

local function FireEvents(Input, InputState)
	local Position = RayCast(Input.Position).Position
	ServerInputPosEvent:FireServer(Position, InputState)
	ClientInputPosEvent:Fire(Position, InputState)
end

UserInputService.InputBegan:Connect(function(Input, GameProcessed)
	if not GameProcessed then
		if Input.UserInputType == Enum.UserInputType.MouseButton1 or Input.UserInputType == Enum.UserInputType.Touch then
			FireEvents(Input, 1)
		end
	end
end)

Try using :ScreenPointToRay instead of ViewportPointToRay

1 Like

It fixed it! Thank you so much! Is it alright if I ask the difference bitween these two?

ScreenPointToRay will start 36 pixels below the top of the screen (height of the topbar/gui inset) and ViewportPointToRay starts at the top of the screen.

Input.Position gives u the screen position, which starts 36 pixels below the top of the screen.

1 Like