If statement brakes loop instead of continuing

Hi developers, I got a problem while making a loop with the if statement

Instead of returning back to the line with if. it will just break the loop, any guesses?

while task.wait(0.05) do
	if script.Parent.Parent.Humanoid:GetAttribute("DelayStamina") > 0 then
		task.wait(0.1)
		script.Parent.Parent.Humanoid:SetAttribute("DelayStamina",script.Parent.Parent.Humanoid:GetAttribute("DelayStamina")-0.1)
		return
	end
	if script.Parent.Parent.Humanoid:GetAttribute("Stamina") <= script.Parent.Parent.Humanoid:GetAttribute("MaxStamina") then
		script.Parent.Parent.Humanoid:SetAttribute("Stamina",script.Parent.Parent.Humanoid:GetAttribute("Stamina")+0.1)
	end
end

return breaks the loop, remove it.

Example usage of return:

local function returnSomething()
return 'sample text'
end
print(returnSomething()) -- 'sample text'

Well, it’s not what I expected.

Seems to be I need to find other solution, but still thanks

did you mean to use

while true do
   if something == something then
      task.wait(.5)
      continue -- goes right back to the top skips anything below 
   end

   task.wait(1)
end

?

or did you want the loop to stop there but still run like this

local function Loop()
    -- use return at some point
end

while true do
Loop()
task.wait(sometime)
end

I want to check if I can restore stamina. If DelayStamina is higher than 0 then it will make another loop until Delay will be 0 again.

oh okay then use the bottom code and sample it from there it should work as intended

I found solution with using for loops instead

while task.wait(0.05) do
	if script.Parent.Parent.Humanoid:GetAttribute("DelayStamina") > 0 then 
		for i = script.Parent.Parent.Humanoid:GetAttribute("DelayStamina"),0,-0.1 do
		    task.wait(0.1)
			script.Parent.Parent.Humanoid:SetAttribute("DelayStamina",script.Parent.Parent.Humanoid:GetAttribute("DelayStamina")-0.1)
		end
	end
	if script.Parent.Parent.Humanoid:GetAttribute("Stamina") <= script.Parent.Parent.Humanoid:GetAttribute("MaxStamina") then
		script.Parent.Parent.Humanoid:SetAttribute("Stamina",script.Parent.Parent.Humanoid:GetAttribute("Stamina")+0.1)
	end
end

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.