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

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

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 comment

1

This is why you should not be using the mouse (and its deprecated) key events. Use the UserInputService which supports controller input.

1 answer

✓ Accepted answer

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

3 comments

0

I can't seem to get the script to work.

0

Made a mistake, sorry. I’ve edited the script so it should work now.

0

Okay, thanks. I was worried I was just being an idiot.

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