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

How would you cycle through weapons?

So, I've got Backpack turned off, and I'm force equipping the tools. It successfully cycles through to the next/previous gun, but it just stops working afterwards. Should I use "while true do" to keep cycling through the weapons?

Here's the snippet of code I've got at the moment:

				local gun1 = game.Lighting.Guns.Runner.SniperRifle
				local gun2 = game.Lighting.Guns.Runner.London
				gun1.Parent = char --Force equip
				if gun1.Parent == char then
					print("Gun1 is parented")
					mouse.WheelForward:connect(function()
						print("Gun2 is parented")
						gun1.Parent = game.Lighting.Guns.Runner
						wait(.5)
						gun2.Parent = char
					end)
					mouse.WheelBackward:connect(function()
						print("Gun2 is parented")
						gun1.Parent = game.Lighting.Guns.Runner
						wait(.5)
						gun2.Parent = char
					end)
				end
				if gun2.Parent == char then
					print("Gun1 is parented")
					mouse.WheelForward:connect(function()
						gun2.Parent = game.Lighting.Guns.Runner
						wait(.5)
						gun1.Parent = char
					end)
				end

3 comments

0

Output shows "SniperRifle is not a valid member of Model"

0

Well first off that is your problem... the sniper rifle isn't part of the model. Use :WaitForChild() or make sure that it is 100% coded to the right hierarchy

0

First thing I would do, to prevent the gun scripts from freaking out, is use a function in the Humanoid called :EquipTool() and :UnequipTools(). All you need to do is call the character's humanoid, add :EquipTool(TOOL_GOES_HERE) and then when you want to unequip, then just call it again and say :UnequipTools()

1 answer

✓ Accepted answer

Ok, so I managed to do a whole lot of editing to your snippet of code, and I found many things that would go wrong.

Firstly, when you parent a gun, it won't be in Lighting, so if another player tried to switch, it would brake. Instead, I cloned the gun into the player's backpack.

Secondly, parenting and unparenting tools from the character can break some guns made from popular gun makers like manofthelol and TurboFusion. Use the :EquipTool() and :UnequipTools() functions in the Humanoid.

Third, you don't need a loop, you need the if statement inside the WheelBackward and WheelForward events.

Here's what I did to your snippet. I will add comments to describe why I did what I did:

game.StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack, false) --Turn the backpack off, I'm assuming you did this
local mouse = game.Players.LocalPlayer:GetMouse() --Get the player's mouse, I'm assuming you did this
local char = game.Players.LocalPlayer.Character --Get the character, I'm assuming you did this
local gun1 = game.Lighting.Guns.Runner.SniperRifle:Clone() --Make a copy of the gun in Lighting to put in the player's backpack
local gun2 = game.Lighting.Guns.Runner.London:Clone() --Make a copy of the gun in Lighting to put in the player's backpack
local Humanoid = char:WaitForChild("Humanoid") --Easily call the player's Humanoid. But we may need to wait a bit, so use :WaitForChild()
gun1.Parent = game.Players.LocalPlayer.Backpack --Take the copied guns and paste them into the player's backpack
gun2.Parent = game.Players.LocalPlayer.Backpack

Humanoid:EquipTool(gun1) --Force equip

function Switch() --This is your main function that will fire every time the player moves his/her mouse wheel up or down
	if gun1.Parent == char then --Check if gun1 is currently equipped
    		print("Gun1 is parented") --Debug
		Humanoid:UnequipTools() --Unequip all the tools that the Humanoid currently has equipped
		wait(.5) --Wait a little bit to make sure it unequips
		print("Gun2 is parented") --Debug
		Humanoid:EquipTool(gun2) --Equip the next gun
	elseif gun2.Parent == char then --Same thing here, just reversed
		print("Gun2 is parented") --Debug
		Humanoid:UnequipTools()
		wait(.5)
		print("Gun1 is parented") --Debug
		Humanoid:EquipTool(gun1)
	end
end

mouse.WheelForward:connect(Switch) --I'm doing it this way so you can switch with both directions
mouse.WheelBackward:connect(Switch)

I tested this to make sure it would work and this is what happened

I made a LocalScript inside the StarterPack for this to work. If the snippet is from a LocalScript and is in StarterPack then you shouldn't have to worry about that.

Good luck with your future Scripting Adventures! ~lightpower26

2 comments

0

Thanks this also helped me with my question too!

0

I get an error saying 'UnequipTools can only be called on a Humanoid with a corresponding Player object.'

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