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

How do you swap tools by scrolling?

Asked by 8 years ago
Edited 8 years ago

I want a script to put into tools so that when I scroll it changes weapons or tools.

I've looked everywhere for this script however I cannot find any youtube tutorials or any websites or wiki's to actually help. I would appreciate someone to teach me how to make one.

0
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 Goulstem 8144 — 8y
0
Thank You So Much! KomradGames 7 — 8y
0
However I don't know what script or where to put it KomradGames 7 — 8y

2 answers

Log in to vote
0
Answered by 8 years ago

Hey, great question.

Now, first off, there are actually a few wiki entries you missed; UserInputService (specifically, InputBegan) and its relations.

The relevant part is the UserInputType, and the Delta (both parts of the InputObject argument from InputBegan's callback). The former has a value, one the potential values being MouseWheel. The latter is a Vector3 describing the movement distance (if there is one), so it would therefore be used to check whether it was being scrolled up or down.

Then we just have a few optimizations and index manipulation to top it all off. The rest of the explanation is in the code.

-- In a LocalScript (Preferably inside StarterCharacter)
local p = game.Players.LocalPlayer
local backpack = p.Backpack
local tools = backpack:GetChildren() -- All the tools
local uis = game:GetService("UserInputService")

local curIndex = 0 -- The current tool's 'position'
local curTool -- Currently equipped tool

function equipTool() -- The function responsible for equipping tools
    local tool = tools[curIndex] -- The new tool
    if curTool then -- The old tool
        curTool.Parent = backpack -- Unequipping it
    end
    tool.Parent = p.Character -- Equipping the new tool
end

ui.InputBegan:connect(function(input,gp)
    if gp == false then -- If the player isn't typing
        if input.UserInputType == Enum.UserInputType.MouseWheel then -- If the player scrolled
            local delta = input.Delta.Y -- The distance the player scrolled (I think it's on the Y axis)
            if delt > 0 then
                curIndex = curIndex + 1 -- Increase the index
                if curIndex > #tools then -- If the index has reached the end, it will go to the start again
                    curIndex = 1 -- Resets to the first tool
                end
            else
                curIndex = curIndex - 1 -- Decrease the index
                if curIndex <= 0 then
                    curIndex = #tools
                end
            end
            equipTool() -- Equips the new tool
        end
    end
end)

for i,v in pairs (tools) do -- Loops through all the tools
    v.Equipped:connect(function() -- When it gets equipped (this is optimizing the script to work with being selected manually)
        curTool = v -- Sets the current tool to this
        if curIndex ~= i then -- The index is not the new tool's index
            curIndex = i -- Sets the index to the correct tool
        end
    end
end

If you need any more explanation, ask.

Hope I helped!

~TDP

Ad
Log in to vote
0
Answered by 8 years ago
Edited 8 years ago

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.

-- Game services
-- We'll be using UserInputService for the scroll wheel
local InputService = game:GetService("UserInputService")
local Players = game:GetService("Players")

-- Local player components
local client = Players.LocalPlayer
local backpack = client:WaitForChild("Backpack") -- Wait for the backpack
local character = client.Character or client.CharacterAdded:Wait() -- This is a nice way to wait for the character to load, if it hasn't already, while also returning it to the variable.

-- Tools container
local tools = backpack:GetChildren() -- This will be our table of tools to work with (doesn't necessarily have to be GetChildren, you could create an empty table and insert the tools yourself)
local toolIndex = 1 -- Our tool index (the key we'll use to index the table for the respective tool associated with it).

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:

-- Some simple math functions we can use to add / subtract certain quantities, or leave blank for a default quantity of 1.

local function add(v, x)
    return v + (x or 1) -- Adds x, or 1.
end

local function sub(v, x)
    return v - (x or 1) -- Subtracts x, or 1.
end

-- We'll also need to make sure whenever we scroll, that we're unequipped the current tool the character is holding. Here we're just iterating the character model, to make sure no additional tools exist.
local function unequipTools()
    for _, item in next, character:GetChildren() do
        if item:IsA("Tool") then
            item.Parent = backpack -- Place it back in the 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:

-- Mouse scroll function
local function mouseScrolled(dir) -- 'dir' is true if mouse scrolls up, false if it scrolls down
    local numTools = #tools -- Get the max number of tools
    unequipTools() -- Make sure the character unequips all other tools first

    -- Change toolIndex based off of scroll direction
    toolIndex = dir and add(toolIndex) or sub(toolIndex)
    -- This will check if the toolIndex is less than 1, or greater than numTools, so that
    -- it gives you the ability to reverse select items
    toolIndex = toolIndex < 1 and numTools or toolIndex > numTools and 1 or toolIndex

    -- Equip the tool
    local tool = tools[toolIndex] -- Locate the tool
    tool.Parent = character -- Parent it to the character (equip it)
end

-- Execute mouse scroll function when user scrolls mouse
-- We can detect the direction of the scroll wheel with UserInputService by comparing it's InputObject's 'Position.Z' value, which will either be -1 (if scrolled down), or +1 (if scrolled up).
InputService.InputChanged:Connect(function(input, processed)
    -- If ROBLOX doesn't already have the user's input focused, then...
    if not processed then
        if input.UserInputType == Enum.UserInputType.MouseWheel then
            mouseScrolled(input.Position.Z > 0) -- (+1) is scroll up, (-1) is scroll down
        end
    end
end)

Combining all of this code together, should give us this final working product:

-- Game services
local InputService = game:GetService("UserInputService")
local Players = game:GetService("Players")

-- Local player components
local client = Players.LocalPlayer
local backpack = client:WaitForChild("Backpack")
local character = client.Character or client.CharacterAdded:Wait()

-- Tools container
local tools = backpack:GetChildren()
local toolIndex = 1

-- Add default value
local function add(v, x)
    return v + (x or 1)
end

-- Subtract default value
local function sub(v, x)
    return v - (x or 1)
end

-- Parent all tools back to the player's backpack
local function unequipTools()
    for _, item in next, character:GetChildren() do
        if item:IsA("Tool") then
            item.Parent = backpack
        end
    end
end

-- Mouse scroll function
local function mouseScrolled(dir) -- 'dir' is true if mouse scrolls up, false if it scrolls down
    local numTools = #tools -- Get the max number of tools
    unequipTools() -- Make sure the character unequips all other tools first

    -- Change toolIndex based off of scroll direction
    toolIndex = dir and add(toolIndex) or sub(toolIndex)
    -- This will check if the toolIndex is less than 1, or greater than numTools, so that
    -- it gives you the ability to reverse select items
    toolIndex = toolIndex < 1 and numTools or toolIndex > numTools and 1 or toolIndex

    -- Equip the tool
    local tool = tools[toolIndex] -- Locate the tool
    tool.Parent = character -- Parent it to the character (equip it)
end

-- Execute mouse scroll function when user scrolls mouse
InputService.InputChanged:Connect(function(input)
    if input.UserInputType == Enum.UserInputType.MouseWheel then
        mouseScrolled(input.Position.Z > 0) -- (+1) is scroll up, (-1) is scroll down
    end
end)

Hope this helped, if you have any questions, just let me know!

Answer this question