Need to polish the code

i made a code that makes player hold a key to sprint but instead of player instanly gains a speed to sprint, it instead gradually gains a speed once he moves
it almost works it mostly a problem player returning to standard speed once he stops

here is the code:


local character = script.Parent
local humanoid = character:WaitForChild("Humanoid")

local MaxSpeed = 25
local MinSpeed = 12
humanoid.WalkSpeed = MinSpeed

UserInputService.InputBegan:Connect(function(input, proccesed)
	if proccesed then return end
	if input.KeyCode == Enum.KeyCode.E then
		-- how it works is that player holds a key and game checks if player is moving 
		-- if not then it repeats minimal speed until player moves then player moves and gains a speed until it reaches maximum speed
		if humanoid.MoveDirection == Vector3.new() then
			repeat
				humanoid.WalkSpeed = MinSpeed
				task.wait()
			until humanoid.MoveDirection ~= Vector3.new()
				
			while humanoid.MoveDirection ~= Vector3.new() and humanoid.WalkSpeed < MaxSpeed do
				humanoid.WalkSpeed += 1
				task.wait(0.1)
			end
		else
			while humanoid.MoveDirection ~= Vector3.new() and humanoid.WalkSpeed < MaxSpeed do
				humanoid.WalkSpeed += 1
				task.wait(0.1)
			end
		end
	end
end)

-- mostly problems comes from here
UserInputService.InputEnded:Connect(function(input, proccesed)
	if proccesed then return end
	if input.KeyCode == Enum.KeyCode.E then
		if humanoid.MoveDirection ~= Vector3.new() then
			while humanoid.WalkSpeed > MinSpeed do
				humanoid.WalkSpeed -= 1
				task.wait(0.1)
			end
		else
			humanoid.WalkSpeed = MinSpeed
		end
	end
end)

-- its just to check if speed is working
while task.wait() do
	print(humanoid.WalkSpeed)
end

note: i just started learning coding like 3 days ago some parts of the code might be confusing

3 Likes

Can you explain what your problem actually is? I didn’t quite understand.

2 Likes

so the problem is

whenever i release a key when i stops, it suppost to instanly change speed to minimal speed but for some reason it doesnt work, instead it gradually slowers

2 Likes

If you want the code to instantly change back to minimal speed then consider removing above and puting this instead:

humanoid.WalkSpeed = MinSpeed
2 Likes

Try this.

local UserInputService = game:GetService("UserInputService")
local character = script.Parent
local humanoid = character:WaitForChild("Humanoid")

local MaxSpeed = 25
local MinSpeed = 12
humanoid.WalkSpeed = MinSpeed

UserInputService.InputBegan:Connect(function(input, proccesed)
	if proccesed then return end
	if input.KeyCode == Enum.KeyCode.E then
		-- how it works is that player holds a key and game checks if player is moving 
		-- if not then it repeats minimal speed until player moves then player moves and gains a speed until it reaches maximum speed
		if humanoid.MoveDirection == Vector3.new() then
			repeat
				humanoid.WalkSpeed = MinSpeed
				task.wait()
			until humanoid.MoveDirection ~= Vector3.new()
				
			while humanoid.MoveDirection ~= Vector3.new() and humanoid.WalkSpeed < MaxSpeed do
				humanoid.WalkSpeed += 1
				task.wait(0.1)
			end
		else
			while humanoid.MoveDirection ~= Vector3.new() and humanoid.WalkSpeed < MaxSpeed do
				humanoid.WalkSpeed += 1
				task.wait(0.1)
			end
		end
	end
end)

-- mostly problems comes from here
UserInputService.InputEnded:Connect(function(input, proccesed)
	if proccesed then return end
	if input.KeyCode == Enum.KeyCode.E then
		
		
			humanoid.WalkSpeed = MinSpeed
		end
	
end)

-- its just to check if speed is working
while task.wait() do
	print(humanoid.WalkSpeed)
end
1 Like

not really what i wanted but still thanks for help

1 Like

What do you mean? Didn’t you say you wanted it to instantly change?

1 Like

whenever I release a key when i dont move not just releasing a key

1 Like

You can use the Humanoid.StateChanged event to know when the player changes their state (walking, running, falling, etc) and if they’re Idle, and the key isn’t held down, then WalkSpeed = MinSpeed.

1 Like