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

I want to make LT the sprint button in my game. How do I do this?

Asked by 6 years ago

I have a sprint script that works completely fine on computers but I don't know how to make it work on Xbox. I want it to work like it does in Jailbreak, where LT is the sprint button.

local mouse = game.Players.LocalPlayer:GetMouse()
local running = false

function getTool()  
    for _, kid in ipairs(script.Parent:GetChildren()) do
        if kid.className == "Tool" then return kid end
    end
    return nil
end


mouse.KeyDown:connect(function (key) -- Run function
    key = string.lower(key)
    if string.byte(key) == 48 then
        running = true
        local keyConnection = mouse.KeyUp:connect(function (key)
            if string.byte(key) == 48 then
                running = false
            end
        end)
        for i = 1,5 do
            game.Workspace.CurrentCamera.FieldOfView = (70+(i*2))
            wait()
        end
        game.Players.LocalPlayer.Character.Humanoid.WalkSpeed = 30
        repeat wait () until running == false
        keyConnection:disconnect()
        game.Players.LocalPlayer.Character.Humanoid.WalkSpeed = 16
        for i = 1,5 do
            game.Workspace.CurrentCamera.FieldOfView = (80-(i*2))
            wait()
        end
    end
end)

As an addition, can anybody tell me why when I press the 0 key it also acts as the shift key? Thanks!

1
This is why you should not be using the mouse (and its deprecated) key events. Use the UserInputService which supports controller input. User#5423 17 — 6y

1 answer

Log in to vote
1
Answered by
LeadRDRK 437 Moderation Voter
6 years ago
Edited 6 years ago

As kingdom5 mentioned, getting keyboard (or any other device's) inputs from the mouse is deprecated and should not be used at all. Instead use UserInputService or ContextActionService to receive inputs from the controller, but I would prefer UIS:

local running = false
local UIS = game:GetService("UserInputService")

function getTool()  
    for _, kid in ipairs(script.Parent:GetChildren()) do
        if kid.className == "Tool" then return kid end
    end
    return nil
end


UIS.InputBegan:Connect(function(inputobj, gpe)
    local key = inputobj.KeyCode
    if key == Enum.KeyCode.ButtonL2 then
        running = true
        for i = 1,5 do
            game.Workspace.CurrentCamera.FieldOfView = (70+(i*2))
            wait()
        end
        game.Players.LocalPlayer.Character.Humanoid.WalkSpeed = 30
    end
end)

UIS.InputEnded:Connect(function(inputobj, gpe)
    local key = inputobj.KeyCode
    if key == Enum.KeyCode.ButtonL2 then
        running = false
        game.Players.LocalPlayer.Character.Humanoid.WalkSpeed = 16
        for i = 1,5 do
            game.Workspace.CurrentCamera.FieldOfView = (80-(i*2))
            wait()
        end
    end
end)

Also notice that I modified other parts of the script aswell; your scripting was inefficient

0
I can't seem to get the script to work. weakcony 2 — 6y
0
Made a mistake, sorry. I’ve edited the script so it should work now. LeadRDRK 437 — 6y
0
Okay, thanks. I was worried I was just being an idiot. weakcony 2 — 6y
Ad

Answer this question