Issue with detecting scrolling direction

Hello, I’ve been trying to detect whether the player scrolls up or down (not gui related as there already is a function for that) in the attempt of making scroll wheel tool switching, but failed.
It is most likely me using InputObject.Position wrong, here is my code so far:

UIS.InputBegan:Connect(function(input, gameProcessed)
	if input.UserInputType == Enum.UserInputType.MouseWheel then
		if input.Position.Z > 0 then
			print("a")
		elseif input.Position.Z < 0 then
			print("b")
		end
	end
end)

I also saw a post related to an engine bug involving input.Position (returning 0.5 instead of 1) so I tried doing if input.Position.Z > 0 then instead of if input.Position.Z == 1 then but still did not seem to work.

Any help would be appreciated.
Thanks.

2 Likes

you must enter:

local UIS = game:GetService("UserInputService") -- as first.

UIS.InputBegan:Connect(function(input, gameProcessed)
if input.UserInputType = Enum.UserInputType.MouseWheel then
if input.Position.Z > 0 then
print("a")
elseif input.Position.Z < 0 then
print("b")
          end
  end
end)
2 Likes

I already had UIS defined as a variable. Thanks for your reply though.

no problem, i didn’t knew that you had the Variable.

Test with the input being 0 if you get any result, if it does not print anything it is more likely an engine bug.

UIS.InputBegan:Connect(function(input, gameProcessed)
	if input.UserInputType == Enum.UserInputType.MouseWheel then
		if input.Position.Z > 0 then
			print("a")
		elseif input.Position.Z < 0 then
			print("b")
		else
			print("RIP you the mouse isn't moving when it is, it makes no sense I know.")
		end
	end
end)

If you do not get any result it might be caused by something else, by the way just to make sure the environment you are testing it in is a LocalScript right.

1 Like

Still nothing in the output, it’s really annoying when that happens.
Yes, I am doing it in a LocalScript.

Now lets do one more test to conclude what is the issue:
First a print outside of the UIS connection
Secondly a print directly inside the UIS connection:

print("Imagine if this wouldn't work.") -- This could be caused by an error stopping the connection from loading
UIS.InputBegan:Connect(function(input, gameProcessed)
	print("I guess the connection does work: ", input, gameProcessed) -- man I dont know at this point like I guess you disconnected it
	if input.UserInputType == Enum.UserInputType.MouseWheel then
		if input.Position.Z > 0 then
			print("a")
		elseif input.Position.Z < 0 then
			print("b")
		else
			print("RIP you the mouse isn't moving when it is, it makes no sense I know.")
		end
	end
end)

(Oh yeah just make sure that the input parameter is valid and you got no errors.)

1 Like

It prints the one below the InputBegan, I also added one below the line that checks if the input is caused by the mouse wheel and it did not print.

This might be an issue with your mouse wheel, let me do some testing of my own…

After testing I have found out that roblox does not detect when you are scrolling, instead I suggest using CAS (local CAS = ContextActionService) or the mouse (local mouse = LocalPlayer:GetMouse()) and go on from there.

Example with mouse:

local LocalPlayer = game:GetService("Players").LocalPlayer
local mouse = LocalPlayer:GetMouse()

local function WheelForward()
end

local function WheelBackward()

end

mouse.WheelForward:Connect(WheelForward)
mouse.WheelBackward:Connect(WheelBackward)

Please mark this post as solution if it helped!

4 Likes

Thanks a lot, it works. Much easier than I thought it would be.