Hi everyone,
I am encountering a very strange issue with a ModuleScript that handles interaction with a object. The system has two main features:
- Hover effect: When the mouse is over the paper, a
SelectionBoxshould become visible (handled inRenderStepped). - Click interaction: When clicking the paper, it tweens to the camera (handled in
InputBegan).
The Problem: The Raycasting logic is erratic.
- InputBegan (Click): Works 99% of the time.
- RenderStepped (Hover): It is extremely inconsistent. Sometimes it works perfectly for a whole session. Other times, I stop the game in Studio, hit Play again, and the Raycast returns
nilcontinuously, even when my mouse is directly over the object.
It seems like a race condition or an initialization issue, but WaitForChild is used for the instances. It’s weird that the exact same Raycast function works for clicks but fails for the hover loop in the same session.
The Setup:
- I am using
RaycastParamswithFilterType.Include. - The target consists of a specific
Paperpart, aHitboxpart, and severalCheckMarkparts (tagged via CollectionService).
The Code:
--!strict
-- Simplified version of the handler
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RunService = game:GetService("RunService")
local UserInputService = game:GetService("UserInputService")
local CollectionService = game:GetService("CollectionService")
-- Variables
local paper = workspace:WaitForChild("Paper") :: BasePart
local paperHitbox = paper:WaitForChild("Hitbox") :: BasePart
local currentCamera = workspace.CurrentCamera
-- Private Function
local function getMouseTarget(filterInstances: {Instance}): Instance | nil
local mouseLocation = UserInputService:GetMouseLocation()
local ray = currentCamera:ViewportPointToRay(mouseLocation.X, mouseLocation.Y)
local params = RaycastParams.new()
params.FilterType = Enum.RaycastFilterType.Include
params.FilterDescendantsInstances = filterInstances
-- Raycast length is 1000 studs
local result = workspace:Raycast(ray.Origin, ray.Direction * 1000, params)
if not result then return nil end
return result.Instance
end
-- Inside the Module:Start() function
function Module:Start()
-- ... (Setup code) ...
-- HOVER CHECK (This is where it fails randomly)
self._trove:Add(RunService.RenderStepped:Connect(function()
-- Try to hit a CheckMark first
local checkTarget = getMouseTarget(self._checksParts)
-- If not, try to hit the Paper or Hitbox
local paperTarget = nil
if not checkTarget then
paperTarget = getMouseTarget({paper, paperHitbox})
end
local target = checkTarget or paperTarget
-- Debugging shows 'target' is nil even when mouse is over the paper
if target then
paperSelectionBox.Transparency = 0.5
else
paperSelectionBox.Transparency = 1
end
end))
-- CLICK CHECK (This usually works fine)
self._trove:Add(UserInputService.InputBegan:Connect(function(input, gameProcessed)
if gameProcessed then return end
if input.UserInputType == Enum.UserInputType.MouseButton1 then
-- Same logic as above, but here it detects the part correctly
local target = getMouseTarget(self._checksParts)
if not target then
target = getMouseTarget({paper})
end
-- Logic to move paper...
end
end))
end