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

Stamina/Sprint function not working properly? Any Ideas?

Alright so I have this stamina/sprint (local) script and its not preforming like I expected... Instead of it actually letting you run, it doesn't..

  1. the while true do loops only runs once?
  2. It wont let you run and stop running?
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 true do
	if(IsSprinting.Value == false) then
		if(script.Parent.Variables.Stamina.Value < 100) then
		wait(.25)
		script.Parent.Variables.Stamina.Value = script.Parent.Variables.Stamina.Value + 1
		script.Parent.GUI.Stamina.Text = "Stamina: "..script.Parent.Variables.Stamina.Value.."%"
		return
		end
	end
end
while true do
	if(IsSprinting.Value == true) then
		if(script.Parent.Variables.Stamina.Value < 0) then
		wait(.05)
		script.Parent.Variables.Stamina.Value = script.Parent.Variables.Stamina.Value - 1
		script.Parent.GUI.Stamina.Text = "Stamina: "..script.Parent.Variables.Stamina.Value.."%"
		return
		end
	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
		end
	end
end)

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

1 answer

You put a return in the while loops, ending them once reached. Just take them out and it should run fine

7 comments

0

Oh, but what about the sprint functions?

0

By 'sprint functions' do you mean the functions connected to the event? If so, returns aren't needed.

0

only use returns to 'Return' a value ex: func(val) return val+1 end print(func(0))

0

No, the function after the while true do

0

Those were the functions I was talking about.

0

Oh how would I fix them?

0

'return' can also be used to return from a function's body prematurely, without returning values.

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