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

KeyDown sprint function not working?

I am making a stamina/sprint script and I made a function that lets you go faster but its not working?

player = script.Parent.Parent.Parent
while true do
	wait(.25)
	if(script.Parent.Variables.Stamina.Value < 100) then
		script.Parent.Variables.Stamina.Value = script.Parent.Variables.Stamina.Value + 1
		script.Parent.GUI.Stamina.Text = "Stamina: "..script.Parent.Variables.Stamina.Value.."%"
	end
end

function onKeyDown(inputObject, gameProcessedEvent)
	if inputObject.KeyCode == Enum.KeyCode.LeftShift then
		if (script.Parent.Variables.Stamina.Value > 0) then
			local humanoid = player.Character:FindFirstChild("Humanoid")
			humanoid.WalkSpeed = 25
		end
	end
end
game:GetService("UserInputService").InputBegan:connect(onKeyDown)

And how would I make it to were when the key goes up, it stops the Sprint

2 comments

0

Sorry I don't have time to answer this, but I can give you something that will help. Here's a video I made on it: https://www.youtube.com/watch?v=9bggLpCFMOg, and the module: https://www.roblox.com/Custom-events-library-Create-or-modify-events-item?id=398814042

0

Please look at my answer below I provided, It adds to the stamina, but It wont increase my speed

1 answer

local InputService = game:GetService("UserInputService")
local Player = game:GetService("Players").LocalPlayer
local Character = Player.Character or Player.CharacterAdded:wait()
local Humanoid = Character:WaitForChild("Humanoid")
local IsSprinting = script.Parent.Variables.IsSprinting

while IsSprinting.Value == false do
	if(script.Parent.Variables.Stamina.Value < 100) then
	IsSprinting.Value = false
	wait(.25)
	script.Parent.Variables.Stamina.Value = script.Parent.Variables.Stamina.Value + 1
	script.Parent.GUI.Stamina.Text = "Stamina: "..script.Parent.Variables.Stamina.Value.."%"
	end
end

InputService.InputBegan:connect(function(Input)
	if Input.KeyCode == Enum.KeyCode.LeftShift then
		if script.Parent.Variables.Stamina.Value > 0 then
			IsSprinting.Value = true
			Humanoid.WalkSpeed = 25
			while IsSprinting.Value == true do
				wait(.05)
				script.Parent.Variables.Stamina.Value = script.Parent.Variables.Stamina.Value - 1
			end
		end
	end
end)

InputService.InputEnded:connect(function(Input)
	if Input.KeyCode == Enum.KeyCode.LeftShift then
		IsSprinting.Value = false
		Humanoid.WalkSpeed = 16
	end
end)

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