Walk/Run Script

Hello everyone,

So, I have alot of scripts in my game so, I was wondering if its possible or how could I make it to make two into one its a local script that in the starterplayer in the StarterPlayerScripts.

so, i want the two scripts to be combined into one script.
First script:

local player = game.Players.LocalPlayer

game:GetService("UserInputService").InputBegan:Connect(function(input, process)
    if input.KeyCode == Enum.KeyCode.H then
        if player.Character:FindFirstChild("Humanoid") then
            player.Character:FindFirstChild("Humanoid").WalkSpeed = 16
        end
    end
end)

second script

local player = game.Players.LocalPlayer

game:GetService("UserInputService").InputBegan:Connect(function(input, process)
    if input.KeyCode == Enum.KeyCode.F then
        if player.Character:FindFirstChild("Humanoid") then
            player.Character:FindFirstChild("Humanoid").WalkSpeed = 20
        end
    end
end)

thats all.

5 Likes
local player = game.Players.LocalPlayer

game:GetService("UserInputService").InputBegan:Connect(function(input, process)
    if input.KeyCode == Enum.KeyCode.H then
        if player.Character:FindFirstChild("Humanoid") then
            player.Character:FindFirstChild("Humanoid").WalkSpeed = 16
        end
    end

    if input.KeyCode == Enum.KeyCode.F then
        if player.Character:FindFirstChild("Humanoid") then
            player.Character:FindFirstChild("Humanoid").WalkSpeed = 20
        end
    end
end)

Should work I think

2 Likes

okay, ill give it a try thank you so much! <3

1 Like

I’m sorry but, it didnt work…

local player = game.Players.LocalPlayer

game:GetService("UserInputService").InputBegan:Connect(function(input, process)
    if input.KeyCode == Enum.KeyCode.H then
        if player.Character:FindFirstChild("Humanoid") then
            player.Character:FindFirstChild("Humanoid").WalkSpeed = 16
        end
    end
    if input.KeyCode == Enum.KeyCode.F then
        if player.Character:FindFirstChild("Humanoid") then
            player.Character:FindFirstChild("Humanoid").WalkSpeed = 20
        end
    end
end)

If that doesnt works, the if input.KeyCode == Enum.KeyCode.F then, change if to elseif. and remove the second end written.

local player = game.Players.LocalPlayer

game:GetService("UserInputService").InputBegan:Connect(function(input, process)
    if input.KeyCode == Enum.KeyCode.H then
        if player.Character:FindFirstChild("Humanoid") then
            player.Character:FindFirstChild("Humanoid").WalkSpeed = 16
        end
    elseif input.KeyCode == Enum.KeyCode.F then
        if player.Character:FindFirstChild("Humanoid") then
            player.Character:FindFirstChild("Humanoid").WalkSpeed = 20
        end
    end
end)
1 Like

Can you paste what you have here? I’m not sure why what I gave you wouldn’t work.

1 Like

You could also use the InputEnded signal / event for the same key instead of relying on 2 keys that set the walkspeed. (i.e: hold H to run, release H to walk again).

You could also use if statements to toggle run / walk, i.e:
if WalkSpeed == 20 then WalkSpeed = 16 end (pseudocode)

Another thing I noticed is that you didn’t check to see if process, which determines if it’s triggered while using a core event, is not true. This may be conflicting, especially when typing in chat.

2 Likes

I’m really sorry it was my mistake I pressed the wrong button. Your script worked and helped me. Thank you so much!! :heart:

1 Like

So there’s a couple minor things you can do a lot better. Let’s start with using ContextActionService for this specific instance, as UserInputService is more for advanced use cases. It’s also easier to implement on other devices.

local Player = game.Players.LocalPlayer
local ContextActionService = game:GetService("ContextActionService")

Now that we have that, let’s make sure to only sprint when the F key is held down, and stop sprinting when released. This will allow you to only use one key and free up the entirely un-needed use of H. Let’s start with making two functions, one that changes the humanoid walkspeed, and the other to be used by input service. I’ll explain why this is important in a second.

local Player = game.Players.LocalPlayer

local ContextActionService = game:GetService("ContextActionService")

local function ChangeWalkspeed(walkspeed)

  --first let's make sure the character and humanoid exists.

  local character = Player.Character

  if character ~= nil then

    local humanoid = character:FindFirstChild("Humanoid")

    if humanoid ~= nil then

      humanoid.WalkSpeed = walkspeed

    end

  end

end

local function SprintHandler(actionName,userInputState,inputObject)

  --this recieves parameters from contextactionservice that we could use.

  if userInputState == Enum.UserInputState.Begin then

    ChangeWalkspeed(25)

  elseif userInputState == Enum.UserInputState.End then

    ChangeWalkspeed(16)

  end

end

As you can see, we do a lot of checks on Walkspeed, this is so we can make sure the character and everything we need exists before doing anything. This allows us to avoid running into errors when let’s say, the player’s character is suddenly deleted. The reason we wrap this in a function is to avoid repeating ourselves, which in general is a frowned-upon practice and one of the big reasons functions exist.

As for the second function, it recieves parameters from ContextActionService that we can use. For instance, we can check if the F key is being either pressed down or released. We can use this to then determine whether what speed we want to give the character, simply by checking if the key was pressed down or released. In our case, let’s go with 25 when pressed down, and 16(default) when released. You may change this at your leisure.

Now, on to the last and simplest stage, let’s simply bind this action, to “Sprint” under the Keycode of F.

ContextActionService:BindAction("Sprint",SprintHandler,true,Enum.KeyCode.F)

Here’s the entire script, feel free to play around with it. There’s definitely a lot to learn from:

local Player = game.Players.LocalPlayer
local ContextActionService = game:GetService("ContextActionService")

local function ChangeWalkspeed(walkspeed)
	--first let's make sure the character and humanoid exists.
	local character = Player.Character
	if character ~= nil then
		local humanoid = character:FindFirstChild("Humanoid")
		if humanoid ~= nil then
			humanoid.WalkSpeed = walkspeed
		end
	end
end

local function SprintHandler(actionName,userInputState,inputObject)
	--this recieves parameters from contextactionservice that we could use.
	if userInputState == Enum.UserInputState.Begin then
		ChangeWalkspeed(25)
	elseif userInputState == Enum.UserInputState.End then
		ChangeWalkspeed(16)
	end
end

ContextActionService:BindAction("Sprint",SprintHandler,true,Enum.KeyCode.F)
9 Likes