Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

What is #IND and why am I obtaining it upon doing math with Mouse Movements?

Asked by 9 years ago

Mouse Movement

As many of you must know, Roblox has a fairly efficient service for handling user input called UserInputService. Now, UserInputService has an event InputChanged which is fired when the input given is changed. I'm trying to use the movement of the mouse to determine as to how much I should damp my actual camera into moving as well. If you take a look at the following code, you will see that I'm trying to determine the angle which needs to be committed to.

local input = game:GetService("UserInputService")
local angle=0;


input.InputChanged:connect(function(inputObj)
    if inputObj.UserInputType == Enum.UserInputType.MouseMovement then
        local deltaySign = inputObj.Delta.y / math.abs(inputObj.Delta.y)
        local deltayMove = (math.abs(inputObj.Delta.y)>=5 and -5 or -1);
        angle = math.min(math.max(angle + (deltayMove * deltaySign), -20), 30)
        print(deltayMove,"------",deltaySign)
        print("Angle",angle)
    end
end)

Issues

When I spawn in test solo, if I keep moving the mouse ONLY UP OR DOWN then the script works perfectly and gives me the angle along with deltaySign and deltayMove.

HOWEVER

When I move my mouse even the slightest bit to either side [left or right], I start getting some weird extension to my numbers: #IND.

Here is an example of output when the code is working and I'm only moving up or down:

Good Output

Here is an example of what I get when the code doesn't work:

Bad Output


Question

So my question is, why am I obtaining #IND as when I move my mouse sideways and what on earth is #IND?

0
Shot in the dark, but could this help? https://goo.gl/ZVFV2p AmiracIe 175 — 9y

1 answer

Log in to vote
3
Answered by 9 years ago

Short Answer: You're dividing by 0 which is creating a NAN value as the deltaySign, which is the value you are getting

Long Answer: In line 7 of your code, you have this line: local deltaySign = inputObj.Delta.y / math.abs(inputObj.Delta.y)

What you're basically doing, is just getting the sign of the Y value, so it'll return either 1 or -1

The problem with this, however, is that inputObj.Delta.y will always have a non-zero value as long as the mouse is moving up and down to some degree. You said the problem occurs whenever the mouse moves side to side.

Essentially what's happening is that because the mouse is moving on the X axis and not the Y axis, there is no change in the Y axis, so inputObj.Delta.y will return 0. That's where the problem starts. When that value is 0, what you're doing is simply 0/0. The numerator and denominator of your fraction will both be 0, so you'll get a NAN

This is a relatively easy fix though. All you need to do is create a simple conditional that will set deltaySign to 0 whenever inputObj.Delta.y is 0, something like this:

local RawdeltaySign = inputObj.Delta.y / math.abs(inputObj.Delta.y)
local FixdeltaySign = (inputObj.Delta.y == 0 and 0 or RawdeltaySign) --This is the fixed value

Basically what the code above will do is "fix" the value and make the value 0 whenever the Y axis delta is 0, which prevents a NAN

Hope this helped!

More info on NAN: NAN

0
Cheers! DigitalVeer 1473 — 9y
Ad

Answer this question