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

How can I change weapons by scrolling?

Asked by 9 years ago
game.Players.PlayerAdded:connect(function(Player)
ting = true
Mouse = Player:GetMouse()
function scroll()
    if ting == true then
    ting = false
    --change weapon
    wait (1)
    ting = true
    end
end)

Mouse.WheelBackward:connect(scroll)
Mouse.WheelForward:connect(scroll)

NOTE: I only need 2 weapons, but I'd like to know if I need to do something different for 3,4,etc...

1
Awesome idea! DewnOracle 115 — 9y

1 answer

Log in to vote
5
Answered by
Goulstem 8144 Badge of Merit Moderation Voter Administrator Community Moderator
9 years ago

To do this then you should follow these steps:

1) - Make a table full of all the tools you want the player to have access to

2) - Make a variable for how you'll index the table.

3) - Change the variable according to the WheelForward and WheelBackward Events

4) Replace the tools in the character with the WheelForward and WheelBackward Events, and index the tool table according to the variable.

Should look something like this;

NOTE: This should be a localscript in StarterGui

local tools = {} --Tool table
local plr = game.Players.LocalPlayer --Player
local mouse = plr:GetMouse() --Mouse
local toolIndex = 1 --Variable for indexing tools table

repeat wait() until plr.Character --Wait for character

local char = plr.Character

function clearTools(c) --Function to remove tools
    for i,v in pairs(c:GetChildren()) do
        if v:IsA('Tool') or v:IsA('HopperBin') then
            v:Destroy()
        end
    end
end

function replaceTool(c)
    local tool = tools[toolIndex]
    if tool then
        clearTools(c)
        tool:Clone().Parent = c
    end
end

mouse.WheelForward:connect(function()
    toolIndex = toolIndex + 1
    replaceTool(char)
end)

mouse.WheelBackward:connect(function()
    toolIndex = toolIndex - 1
    replaceTool(char)
end)
0
Thank you so much. I didn't post this question, but I'm going to use the idea of your code for my script, which I'm creating so that you can use the bumper buttons on a gamepad to equip tools. yumtaste 476 — 9y
0
Thanks! I've been trying to figure out how to force the first tool to be selected when I spawn, but I can't quite figure it out. If you don't mind helping with that. BSIncorporated 640 — 9y
0
Parent it to the character and set the property of the tool 'CanBeDropped' to false(: Goulstem 8144 — 9y
0
I know i'm late but i hope you can answer this. Does this still work properly? I'm desperate for it. eromusics 10 — 4y
Ad

Answer this question