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

How can i make my character walk faster with user input service?

Hey,i tried to make my character walk faster using "User input service" to make a sprint key.

local UIS = game:GetService("UserInputService")

UIS.InputBegan:connect(function(input)
	if input.KeyCode == Enum.KeyCode.Q then
		print("yes")
		local hum = game.Players.LocalPlayer:WaitForChild("Humanoid")
		hum.WalkSpeed = 25
	end
end)

This is my code. I tested it and when i'm pressing q it does print "yes" and i'm 100% sure that the 6 and 7th lines aren't correct. Could someone help me?

1 comment

0

also the script is a local one so there are no tehnnical issues

2 answers

✓ Accepted answer
local UIS = game:GetService("UserInputService")

UIS.InputBegan:connect(function(input)
    if input.KeyCode == Enum.KeyCode.Q then
        print("yes")
        local hum = game.Players.LocalPlayer.Character:WaitForChild("Humanoid")
        hum.WalkSpeed = 25
    end
end)

UIS.InputEnded:connect(function(input)
    if input.KeyCode == Enum.KeyCode.Q then
        print("yes2")
        local hum = game.Players.LocalPlayer.Character:WaitForChild("Humanoid")
        hum.WalkSpeed = 16
    end
end)
  1. When you are making such a variable (like hum) then it's best to use it in the global scope, so put hum before the InputBegan since it doesn't need any new data that the function just gave you.

  2. You are waiting for Humanoid in the LocalPlayer and not in the Character.

  3. You seem to have forgot about also doing the same exact thing but for when the player stops pressing Q. You would use InputEnded, it's the exact same but don't forget to change WalkSpeed back to 16 so they stop sprinting.

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