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

I need a game pad code to select a tool when a button is pressed, but how?

Ok, so far I have created a script that will change the players walkspeed if they hold the L3 button on a gamepad. Next, I need the code to select a tool when pressed. If I press ButtonY, then it should select tool1. To be honest, all I need is for someone to tell me how to select a tool if a certain button is pressed, or an event occurs. I've come up with this so far:

--Left ThumbStick(Click) - Sprint
-- Make variables for services
local userInputService = game:GetService("UserInputService")
local runService = game:GetService("RunService")
 
-- Make variables for player, character, and camera
local player = game.Players.LocalPlayer
while not player.Character do wait() end
local character = player.Character

userInputService.InputEnded:connect(function(input, processed)
	if input.UserInputType == Enum.UserInputType.Gamepad1 then
		if input.KeyCode == Enum.KeyCode.ButtonL3 then
			character.Humanoid.WalkSpeed = 16
		end
	end
end)

userInputService.InputBegan:connect(function(input, processed)
	if input.UserInputType == Enum.UserInputType.Gamepad1 then
		if input.KeyCode == Enum.KeyCode.ButtonL3 then
			character.Humanoid.WalkSpeed = 30
		end
	end
end)

userInputService.InputChanged:connect(function(input, processed)
	if input.UserInputType == Enum.UserInputType.Gamepad1 then
		if input.KeyCode == Enum.KeyCode.ButtonY then
-- this is where the tool selection part would go
		end
	end
end)

Thanks for any help!

1 answer

✓ Accepted answer

Well, you could easily use the EquipTool event

game.Players.LocalPlayer.Character.Humanoid:EquipTool(tool)

So it will look like

userInputService.InputChanged:connect(function(input, processed)
local tool = game.Players.LocalPlayer.Backpack:FindFirstChild("Tool Name")
    if input.UserInputType == Enum.UserInputType.Gamepad1 then
        if input.KeyCode == Enum.KeyCode.ButtonY then
	game.Players.LocalPlayer.Character.Humanoid:EquipTool(tool)
        end
    end
end)

Hope it helped, if not comment on this!

Thanks, -Lost

5 comments

0

So where it says (tool) do I put the tool name in the brackets?

0

It's not working, sorry.

0

Sorry, I found what was going wrong. You change InputChanged to InputBegan. My bad! Thanks!

0

Is there a way to unequip the tool?

0

Found it. It's UnequipTools(). Cheers!

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