Attempt to perform arithmetic (sub) on number and function

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)

Oh by the way, the error is on currentstamina.Value, where it has os.clock in it and stuff

because youre setting the lastMoved variable to the os.clock function

local lastMoved = os.clock()
local lastMoved = os.clock()

lol you beat me to it

char limit

So how would i fix this?

char limit idjaoijsdawduiaw

just replace the line with this:

local lastMoved = os.clock()

Ty!! But now it instantly regenerates without waiting the 3 seconds

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)
1 Like

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