Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Why is this sprint script not working?

ouse.KeyDown:connect(function(key) 
	if sprint > 0 then
		sprinting = true
	end	
	while sprinting == true  do
		if key == string.char(48) and player.Character and player.Character.Humanoid and sprint > 0 then
			player.Character.Humanoid.WalkSpeed = 23
			sprint = sprint - 1
			wait(.01)
			gui3:TweenSize(UDim2.new((bnumber * sprint),0,0.283,0),"Out", "Back", 1,true)
		elseif sprint <= 0 then
			player.Character.Humanoid.WalkSpeed = 16
			sprinting = false
			return
			wait()
		end	
		wait()
	end	
end)

ouse.KeyUp:connect(function(key)
	if key == string.char(48) and player.Character and player.Character.Humanoid then
		player.Character.Humanoid.WalkSpeed = 16
		sprinting = false
	end
end)

When the shift key is released the person remains sprinting, I am not sure why.

2 comments

0

Don't forget to upvote and accept my answer. :P

0

I already upvoted (was at -1 previously)

1 answer

✓ Accepted answer

Since KeyUp and KeyDown should by all right be deprecated at this point, I'm going to rewrite your code to use the UserInputService.

Basically, I put all the code that actually control your WalkSpeed into one place, and had everything else just modify the "sprinting" value. I also couldn't really see why your code didn't work.

local uis = game:GetService("UserInputService") --Obviously in a LocalScript
local sprinting = false
uis.InputBegan:connect(function(inst)
	if inst.KeyCode == Enum.KeyCode.LeftShift then
		if not player.Character then return end
		local human = player.Character:FindFirstChild("Humanoid")
		if not human then return end
		sprinting = sprint > 0
		while sprinting do
			sprinting = sprint > 0
			human.WalkSpeed = 23
			sprint = sprint - 1
			wait(.01)
			gui3:TweenSize(UDim2.new((bnumber * sprint),0,0.283,0),"Out", "Back", 1,true)
		end
		human.WalkSpeed = 16
	end
end)
uis.InputEnded:connect(function(inst)
	if inst.KeyCode == Enum.KeyCode.LeftShift then
		sprinting = false
	end
end)

2 comments

1

you should explain what changed

1

Sorry for the late reply but this helped me A LOT, I was very confused about inputservice but I got it now, thanks

This discussion is preserved as it was. New answers, comments, and votes are closed.