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.
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
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
That method works, but it can be unreliable in some situations.
It may not be instant, because you are using a While loop It cannot always be instant due to you needing a delay.
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.
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.
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!