How to fix this shift to sprint script

Hello im a beginner scripter and i just got to alvin bloxes 2nd advanced scripting tutorial and with (almost) no help at all i made this script


But the only problem that i found with it is that when you press a or d when holding shift it stops going fast is there any way to fix that?
Also I want it to be a hold down shift to sprint.

use UIS:IsKeyDown() to see if shift is being pressed, if yes then increase the speed, if no then speed back to normal

1 Like

Ok but where do i put that? Because i havent learned that yet and i dont know where to put it.

Just do it in both InputBegan and InputEnded, or you can do it in a while loop as well

Like this?


Theres no input end

heres a quick code I made, put this in StarterPlayer>StarterCharacterScripts

local UIS = game:GetService("UserInputService")

local hum = game.Players.LocalPlayer.Character.Humanoid

while true do
	wait()
	if UIS:IsKeyDown(Enum.KeyCode.LeftShift) then
		hum.WalkSpeed = 30
	else
		hum.WalkSpeed = 16
	end
end

UIS is just short for UserInputService, and IsKeyDown is not an event that is fired, so you cant connect it to a function. And I don’t think you can make a sprint script with only InputBegan, since you have no way of knowing when shift is released

1 Like

Thank you! It works perfectly now! I used to think these scripts were really complecated!

Once you figure out the game logic, the scripting is actually the easier part. I suggest you take a look at the api references so you can find what functions to use in different situations, instead of relying on youtube videos which sometimes don’t fully explain things well. For example this page contains pretty much everything you need to know about UserInputService, with all the functions and such. It is a really useful resource and solves my problems 90% of the time

2 Likes

That method works, but it can be unreliable in some situations.

  1. It may not be instant, because you are using a While loop It cannot always be instant due to you needing a delay.
  2. Using wait() with no parameter is a bad coding practice in Roblox. It lags up your game. Use RunService events. RenderStepped for client, Heartbeat for server. Plus, wait() was used in 30 FPS Roblox. Nowadays, we have 60 FPS Roblox.
game:GetService("RunService").Heartbeat:Wait() -- Replacement to wait() on server
game:GetService("RunService").RenderStepped:Wait() -- Replacement to wait() on client

Here is a better method for your sprint script. You made one little mistake. If the Player starts pressing W, input would end right away.

My method uses ContextActionService, the way that Roblox recommends handling things. I actually find it easier to use, and is better with optimizing code, and making it faster. It only fires when you actually press the key / button you want.

local ContextActionService = game:GetService("ContextActionService")
local Players = game:GetService("Players")

local Player = Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait() -- We are adding this :Wait() in to make sure the character exists before we do anything else.
local Humanoid = Character:WaitForChild("Humanoid")

local function Sprint(ActionName, InputState, InputObject)
	if InputState == Enum.UserInputState.Begin then
		Humanoid.WalkSpeed = 50 -- Run Speed
	else
		Humanoid.WalkSpeed = 16 -- Default speed
	end
end

ContextActionService:BindAction("Sprint", Sprint, false, Enum.KeyCode.LeftShift)

Now lets say you wanted to add mobile support to it? Well, luckily, I can add it:

local ContextActionService = game:GetService("ContextActionService")
local Players = game:GetService("Players")

local Player = Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait() -- We are adding this :Wait() in to make sure the character exists before we do anything else.
local Humanoid = Character:WaitForChild("Humanoid")

local function Sprint(ActionName, InputState, InputObject)
	if InputState == Enum.UserInputState.Begin then
		Humanoid.WalkSpeed = 50 -- Run Speed
	else
		Humanoid.WalkSpeed = 16 -- Default speed
	end
end

ContextActionService:BindAction("Sprint", Sprint, true, Enum.KeyCode.LeftShift)
ContextActionService:SetTitle("Sprint", "Sprint")
ContextActionService:SetPosition("Sprint", UDim2.new(1, -70, 0, 10))

That is true but he is obviously new. You think someone who barely knew about userinputservice and didnt even know that InputEnded existed can understand your code? Ik while loops are bad practice but he isn’t making an AAA game, just practicing and learning, so for his purposes a while loop is simple and completely fine.

3 Likes

Thats true this is just a testing game for learning about scripting that i made nothing special.

1 Like

Yea. I was just telling you so you wouldn’t end up like me…

1 Like

Well do you have any videos or documents to recommend about that? Because i just started watching video tutorials about scripting litterally yesterday i spent 8 hours today finishing the beginner series.

TheDevKing posts really good content! Some of his newer ones are also pretty funny!

1 Like

Thanks for recommending that actually i was gonna watch some other tutorials other than alvin blox because he only has 4 advanced tutorials and ive already watch 2 thanks!

1 Like