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

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

Asked by 8 years ago

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

Log in to vote
0
Answered by 8 years ago

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

0
So where it says (tool) do I put the tool name in the brackets? jamesbiggsbot 0 — 8y
0
It's not working, sorry. jamesbiggsbot 0 — 8y
0
Sorry, I found what was going wrong. You change InputChanged to InputBegan. My bad! Thanks! jamesbiggsbot 0 — 8y
0
Is there a way to unequip the tool? jamesbiggsbot 0 — 8y
0
Found it. It's UnequipTools(). Cheers! jamesbiggsbot 0 — 8y
Ad

Answer this question