I wonder if anyone can explain each part of this part of the script? Thank you.
UserInputService.InputChanged:connect(function(inputObject, processed)
if not processed then
if inputObject.UserInputType == Enum.UserInputType.MouseWheel then
local newSlot = currentSlot - inputObject.Position.Z
if newSlot > BACKPACK_SIZE then
newSlot = 1
elseif newSlot < 1 then
newSlot = BACKPACK_SIZE
end
SetSlot(newSlot)
end
end
end)
Important Information: I don’t know how to script but, I might know how to modify some code and I might be able to modify some code.
This seems to be the “scrolling” part of an inventory script. When the player moves their mouse wheel, the selected inventory slot will be changed.
Here’s every part explained:
This “connects” a function to the InputChanged event. This means that the specified function (basically the rest of the code you sent) runs whenever user’s (player’s) input changes.
These are 2 checks to make sure that it is the right type of input.
The function itself runs whenever any input changes, but we only want to run the rest of the code if the input changing is the mouse wheel.
if not processed then
if inputObject.UserInputType == Enum.UserInputType.MouseWheel then
This part seems to evaluate the position inside the inventory (“backpack”). If you scroll to the end of the inventory, the slot will be set back to slot 1 which is the start.
local newSlot = currentSlot - inputObject.Position.Z
if newSlot > BACKPACK_SIZE then
newSlot = 1
elseif newSlot < 1 then
newSlot = BACKPACK_SIZE
end
This last line is calling a function called “SetSlot”. I don’t know what it does because it’s not a part of the script snippet you sent.
I have a question, If I change the if inputObject.UserInputType == Enum.UserInputType.MouseWheel then to if inputObject.UserInputType == Enum.KeyCode.E then, will it work?
In this case, the slot would be changed whenever E is pushed down, but also when it’s released. So for every click you would move 2 slots.
But technically it would work…