I’m trying to use UserInputService to shoot a raycast every time the mouse moves on screen. Seems simple, right? Well, for some reason, the mouse movement only registers after I move my mouse offscreen and back on again.
Recording of Bug
The code looks like this:
local Navigation = {}
Navigation.__index = Navigation
local UserInputService = game:GetService("UserInputService")
local Part = workspace.PartThingy
function Navigation.new()
local self = {
Connections = {}, -- cleanup blahblahblah
PartClone = generatePart() -- a literal cloned part in the workspace
}
setmetatable(self, Navigation)
return self
end
function Navigation:Activate()
table.insert(self.Connections, UserInputService.InputBegan:Connect(function(input: InputObject, inGui: boolean)
if inGui then return end
if input.UserInputType == Enum.UserInputType.MouseMovement then
-- Updates the position of the PartClone depending on the mouse's position.
-- Works whenever Roblox decides to run it
self:UpdateMovement()
elseif input.UserInputType == Enum.UserInputType.MouseButton1 then
-- Places the mouse-following PartClone and makes another. Works fine.
self.PartClone.Anchored = true
self.PartClone.Name = "PLACED_PART"
self.PartClone.Transparency = 0
self.PartClone = generatePart()
end
end))
end
function generatePart() -- Responsible for making the PartClone
local PartClone = Part:Clone()
PartClone.Anchored = false
PartClone.Transparency = 0.5
PartClone.Name = "RayCast Visualizer"
PartClone.Parent = workspace
PartClone.BrickColor = BrickColor.Random()
return PartClone
end
function Navigation:UpdateMovement()
local mouseLocation = UserInputService:GetMouseLocation()
local mouseRay = Camera:ViewportPointToRay(mouseLocation.X, mouseLocation.Y)
local raycastRules = RaycastParams.new()
raycastRules.FilterType = Enum.RaycastFilterType.Exclude
raycastRules.FilterDescendantsInstances = {Player.Character, Part, self.PartClone}
-- Reminder: Part is the original Part the self.PartClone is cloned from
local MouseRayResult = workspace:Raycast(mouseRay.Origin, mouseRay.Direction * 400, raycastRules)
if MouseRayResult then
self.PartClone.Position = MouseRayResult.Position
else
warn("Raycast... failed????")
end
end
function Navigation:Destroy()
for connection:RBXScriptSignal in self.Connections do
connection:Disconnect()
end
end
return Navigation
If I put a breakpoint here…
function Navigation:Activate()
table.insert(self.Connections, UserInputService.InputBegan:Connect(function(input: InputObject, inGui: boolean)
if inGui then return end -- BREAKPOINT HERE
--More Code...
The breakpoint still only goes off when I move my mouse from offscreen back to onscreen again. I am completely dumbfounded and have no idea how to fix this. Any suggestions?
EDIT: Forgot to add Navigation and UserInputService variables to the OG Code example. Don’t worry-- they were always there in my original code