Hello! Im making it so that whenever the player stops moving, the stamina rises, but this isn’t working. There are no errors in the output. Can someone please help me? I would really appreciate it.
Thank you.
game.Players.PlayerAdded:Connect(function(plr)
local char = plr.Character or plr.CharacterAdded:Wait()
local currentstamina = char:FindFirstChild("Stamina")
while task.wait(1) do
if plr.Character.Humanoid.MoveDirection == Vector3.new(0, 0, 0) then
if currentstamina.Value == 100 then
return
elseif currentstamina.Value < 101 then
char:FindFirstChild("Stamina").Value += 1
end
else
return
end
end
end)
Well, first off, when the player reaches 100 stamina, you use return, so it wont run for that player at all anymore, as return completely stops all loops its in.
Next, when the play IS moving, you return again, causing the issue mentioned above.
The code below should work, or at least be a lot closer to working
game.Players.PlayerAdded:Connect(function(plr)
local char = plr.Character or plr.CharacterAdded:Wait()
local currentstamina = char:FindFirstChild("Stamina")
while task.wait(1) do
if plr.Character.Humanoid.MoveDirection == Vector3.new(0, 0, 0) then
if currentstamina.Value < 100 then
char:FindFirstChild("Stamina").Value += 1
end
end
end
end)
So for future, dont use return unless you want to entirely stop the loop
For that you’ll want to add a variable for time since moved at the beginning of the event:
--Gets the current time
local lastMoved = os.clock()
Then check if they moved (which you already are), and if they did reset it
Finally, add the check for time since moved
if plr.Character.Humanoid.MoveDirection == Vector3.new(0, 0, 0) then
if currentstamina.Value < 100 and os.clock()-lastMoved > (seconds they have to wait) then
char:FindFirstChild("Stamina").Value += 1
end
else
lastMoved = os.clock()
end