local mousePositionX
local mousePositionY
local gameProcessed
function createRay()
if mousePositionX and gameProcessed == false then
local unitray = workspace.Camera:ViewportPointToRay(mousePositionX,mousePositionY)
local ray = Ray.new(unitray.Origin, unitray.Direction * 500)
local hit = workspace:FindPartOnRayWithIgnoreList(ray,IgnoreList)
if hit then
game.ReplicatedStorage.print:FireServer(hit.Name)
selBox.Adornee = hit
end
end
end
function findHover(input,gameProcessedEvent)
if debounceMouse == false and input.UserInputType == Enum.UserInputType.MouseMovement and not gameProcessedEvent or debounceMouse == false and input.UserInputType == Enum.UserInputType.Touch and not gameProcessedEvent then
debounceMouse = true
mousePositionX = input.Position.X
mousePositionY = input.Position.Y
gameProcessed = false
createRay()
debounceMouse = false
elseif gameProcessedEvent then
gameProcessed = true
end
end
Hey, I’ve heard that using UserInputService is more effective and has advantages. the script ran on Player:GetMouse() before but I’ve decided to rewrite it using UIS as a learning experience.
Hmm, you were right about the Topbar 36 pixels, so I’ve just added 36 to the mousePositionY and now it works!
Here’s the updated portion of the code:
function findHover(input,gameProcessedEvent)
if debounceMouse == false and input.UserInputType == Enum.UserInputType.MouseMovement and not gameProcessedEvent or debounceMouse == false and input.UserInputType == Enum.UserInputType.Touch and not gameProcessedEvent then
debounceMouse = true
mousePositionX = input.Position.X
mousePositionY = input.Position.Y + 36
gameProcessed = false
createRay()
debounceMouse = false
elseif gameProcessedEvent then
gameProcessed = true
end
Just as an FYI, you could also replace ViewportPointToRay with ScreenPointToRay to accomplish this without adding 36px to the mouse position. The only difference being, as the devhub states, that ScreenPointToRay accounts for the topbar.