Shift to run script that transitions Walking speed to Running speed

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? I want to transition the walking speed (10) to run speed (25) for example in criminality when you hold shift and when you let go the running speed transitions to the walking speed.

  2. What is the issue? My script doesn’t work and there are no errors.

  3. What solutions have you tried so far? I have looked around on the dev forum and tried different things but I can’t seem to find a solution.

Kinda embarrassing seeing how this is my second dev forum post today. :sweat_smile:

local uis = game:GetService("UserInputService")
local plr = game.Players.localPlayer
local character = plr.Character or plr.CharacterAdded:Wait()
local Humanoid = character:WaitForChild("Humanoid")
local runservice = game:GetService("RunService")

walkspeed = 10
--speed up

uis.InputBegan:Connect(function(input)
	if input.UserInputType == Enum.UserInputType.Keyboard then
		if input.KeyCode == Enum.KeyCode.LeftShift then
			for i = 10, 25,1 do
				runservice.Heartbeat:Wait()
				walkspeed = i
			end
		end
	end
end)


uis.InputEnded:Connect(function(input)
	if input.UserInputType == Enum.UserInputType.Keyboard then
		if input.KeyCode == Enum.KeyCode.LeftShift then
			for i = 25, 10,1 do
				runservice.Heartbeat:Wait()
				walkspeed = i
			end
		end
	end
end)

hey,
I’d recommend using TweenService instead of for loops, this should work perfectly for your case

Idk how to use the tweenservice though.

local uis = game:GetService("UserInputService")
local plr = game.Players.localPlayer
local character = plr.Character or plr.CharacterAdded:Wait()
local Humanoid = character:WaitForChild("Humanoid")
local runservice = game:GetService("RunService")

uis.InputBegan:Connect(function(input)
	if input.UserInputType == Enum.UserInputType.Keyboard then
		if input.KeyCode == Enum.KeyCode.LeftShift then
			game.TweenService:Create(Humanoid, TweenInfo.new(1), {WalkSpeed = 25}):Play()
		end
	end
end)


uis.InputEnded:Connect(function(input)
	if input.UserInputType == Enum.UserInputType.Keyboard then
		if input.KeyCode == Enum.KeyCode.LeftShift then
			game.TweenService:Create(Humanoid, TweenInfo.new(1), {WalkSpeed = 10}):Play()
		end
	end
end)

instead of using a for loop, why not just use tween service?
also walkspeed didn’t appear to ever be defined.
and you were doing a wait on something that was already loaded so i assume that would cause an infinite wait.

just learn it, TweenService is important and you will definitely not regret learning it

It works now, thank you so much!

Also, thanks for the tip. I’ll make sure to learn it!

1 Like