Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How can I change weapons by scrolling?

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 comment

1

Awesome idea!

1 answer

✓ Accepted answer

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)

4 comments

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.

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.

0

Parent it to the character and set the property of the tool 'CanBeDropped' to false(:

0

I know i'm late but i hope you can answer this. Does this still work properly? I'm desperate for it.

This discussion is preserved as it was. New answers, comments, and votes are closed.