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

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

Asked by
seikkatsu 110
5 years ago

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?

0
also the script is a local one so there are no tehnnical issues seikkatsu 110 — 5y

2 answers

Log in to vote
1
Answered by 5 years ago
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)
Ad
Log in to vote
0
Answered by 5 years ago

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.

Answer this question