I’m trying to be a cool guy and use a modulescript to implement a thing I’m working on, but I’m brain farting on something I’m sure is super simple. The module itself will need to reassign a module variable anytime the mouse moves, so I used RunService and UserInputService:GetMouseLocation() to detect when the mouse moves and reassign the variable like that. The current implementation looks somewhat like this:
function module.new()
local self = {
activated = false,
location = Vector3.new(0,0,0)
}
setmetatable(self, module)
self:Activate() -- only here for testing
return self
end
function module:Activate()
RunService:BindToRenderStep("movement", Enum.RenderPriority.Input.Value, function ()
self:UpdateMovement()
end)
self.activated = true
end
function module:UpdateMovement()
local newLocation = UserInputService:GetMouseLocation()
if newLocation.Magnitude - self.location.Magnitude == 0 then -- not actually tested to work, but you get the gist
print("no movement")
return
end
self.location = newLocation
-- more stuff...
end
function module:Deactivate()
if self.activated then
RunService:UnbindToRenderStep("movement")
end
end
function module:Destroy()
self:Deactivate()
end
The above works, but running something every frame is wasteful when I only want to run something when an input happens. So, I’ve been looking to migrate the above to UserInputService:InputBegan
. However, I have no idea how to do that. Not because I don’t know how to use InputBegan
but because I need to change self.location
inside of the connect when it’s not in scope. Am I missing something stupid or should I just say screw it and march on with what I already have?