All you'd need to do is create a container for your tools (as a table), and have some sort of index variable change depending on the direction of your scroll wheel's direction. This 'index variable' is the key to locating which tool is next, or behind in the selection queue.
First, you should start off by declaring some important, reusable data. This should include game services, information about the player, and data your program will use regularly.
local InputService = game:GetService("UserInputService")
local Players = game:GetService("Players")
local client = Players.LocalPlayer
local backpack = client:WaitForChild("Backpack")
local character = client.Character or client.CharacterAdded:Wait()
local tools = backpack:GetChildren()
local toolIndex = 1
There's also a few utility functions we can create that'll come in handy for the actual mouse scrolling function, and changing the tool index. That'd be good to declare next:
local function add(v, x)
return v + (x or 1)
end
local function sub(v, x)
return v - (x or 1)
end
local function unequipTools()
for _, item in next, character:GetChildren() do
if item:IsA("Tool") then
item.Parent = backpack
end
end
end
Now we can get to the heart of our script - creating the mouse scroll function, and connecting it to the UserInputService listener. This will use all of the methods I explained at the beginning of this answer:
local function mouseScrolled(dir)
local numTools = #tools
unequipTools()
toolIndex = dir and add(toolIndex) or sub(toolIndex)
toolIndex = toolIndex < 1 and numTools or toolIndex > numTools and 1 or toolIndex
local tool = tools[toolIndex]
tool.Parent = character
end
InputService.InputChanged:Connect(function(input, processed)
if not processed then
if input.UserInputType == Enum.UserInputType.MouseWheel then
mouseScrolled(input.Position.Z > 0)
end
end
end)
Combining all of this code together, should give us this final working product:
local InputService = game:GetService("UserInputService")
local Players = game:GetService("Players")
local client = Players.LocalPlayer
local backpack = client:WaitForChild("Backpack")
local character = client.Character or client.CharacterAdded:Wait()
local tools = backpack:GetChildren()
local toolIndex = 1
local function add(v, x)
return v + (x or 1)
end
local function sub(v, x)
return v - (x or 1)
end
local function unequipTools()
for _, item in next, character:GetChildren() do
if item:IsA("Tool") then
item.Parent = backpack
end
end
end
local function mouseScrolled(dir)
local numTools = #tools
unequipTools()
toolIndex = dir and add(toolIndex) or sub(toolIndex)
toolIndex = toolIndex < 1 and numTools or toolIndex > numTools and 1 or toolIndex
local tool = tools[toolIndex]
tool.Parent = character
end
InputService.InputChanged:Connect(function(input)
if input.UserInputType == Enum.UserInputType.MouseWheel then
mouseScrolled(input.Position.Z > 0)
end
end)
Hope this helped, if you have any questions, just let me know!
3 comments
You can search for your question before asking to see if it has been answered before on this site ;D https://scriptinghelpers.org/questions/16860
Thank You So Much!
However I don't know what script or where to put it