Hello! Im working on a Stamina Bar right now and it detects when a player stops moving, waits 3 seconds, and refills it. However, if the player moves between the 3 seconds, the timer resets.
It keeps giving me this weird error, and I don’t know why. Can someone help?
Thank you.
game.Players.PlayerAdded:Connect(function(plr)
local char = plr.Character or plr.CharacterAdded:Wait()
local hum = char.Humanoid or char:WaitForChild("Humanoid")
local currentstamina = char:WaitForChild("Stamina") or char:FindFirstChild("Stamina")
local lastMoved = os.clock
while task.wait(1) do
if plr.Character.Humanoid.MoveDirection ~= Vector3.new(0, 0, 0) and char:FindFirstChild("Blocking").Value == false then
if currentstamina.Value < 100 and os.clock()-lastMoved > 3 then
currentstamina.Value += 2
end
elseif plr.Character.Humanoid.MoveDirection ~= Vector3.new(0, 0, 0) and char:FindFirstChild("Blocking").Value == true then
if currentstamina.Value < 100 and os.clock()-lastMoved > 3 then
currentstamina.Value += 5
end
elseif plr.Character.Humanoid.MoveDirection == Vector3.new(0, 0, 0) and char:FindFirstChild("Blocking").Value == false then
if currentstamina.Value < 100 and os.clock()-lastMoved > 3 then
currentstamina.Value += 3
end
else
lastMoved = os.clock()
end
end
end)
i dont exactly get what youre trying to achieve but try this:
game.Players.PlayerAdded:Connect(function(plr)
local char = plr.Character or plr.CharacterAdded:Wait()
local hum = char.Humanoid or char:WaitForChild("Humanoid")
local currentstamina = char:WaitForChild("Stamina") or char:FindFirstChild("Stamina")
local lastMoved = os.clock()
hum:GetPropertyChangedSignal("MoveDirection"):Connect(function()
lastMoved = os.clock()
end)
while task.wait(1) do
if plr.Character.Humanoid.MoveDirection ~= Vector3.new(0, 0, 0) and char:FindFirstChild("Blocking").Value == false then
if currentstamina.Value < 100 and os.clock()-lastMoved > 3 then
currentstamina.Value += 2
end
elseif plr.Character.Humanoid.MoveDirection ~= Vector3.new(0, 0, 0) and char:FindFirstChild("Blocking").Value == true then
if currentstamina.Value < 100 and os.clock()-lastMoved > 3 then
currentstamina.Value += 5
end
elseif plr.Character.Humanoid.MoveDirection == Vector3.new(0, 0, 0) and char:FindFirstChild("Blocking").Value == false then
if currentstamina.Value < 100 and os.clock()-lastMoved > 3 then
currentstamina.Value += 3
end
end
end
end)