Buggy stamina value?

i have this script that will change the stamina value. So far it works but there are a few bugs

it’ll go in the negatives and such and idk how to fix it. This is my first time making a stamina script from scratch so im having some trouble

heres the script
–Client

UIS.InputBegan:Connect(function(input, chat)
	if chat then return end
	
	if UIS:IsKeyDown(Enum.KeyCode.LeftShift) and input.KeyCode == Enum.KeyCode.W or input.KeyCode == Enum.KeyCode.A or input.KeyCode == Enum.KeyCode.S or input.KeyCode == Enum.KeyCode.D then
		local isKeyCown = UIS:IsKeyDown(Enum.KeyCode.LeftShift)
		startup = event:InvokeServer({"Running", isKeyCown})
		local tween = TS:Create(camera, TweenInfo.new(.5), {FieldOfView = 90})
		tween:Play()
	end
end)

UIS.InputEnded:Connect(function(input, chat)
	if chat then return end
	
	if input.KeyCode == Enum.KeyCode.LeftShift  and not UIS:IsKeyDown(Enum.KeyCode.LeftShift) then
		if startup == true then
			local runEnded = event:InvokeServer({"Not Running"})
			if runEnded	== true then
				local tween = TS:Create(camera, TweenInfo.new(.5), {FieldOfView = 70})
				tween:Play()
				print(1)
			end
		end
	end
end)

–Server

event.OnServerInvoke = function(plr, args)
	if plr then
		local char = plr.Character
		local hum = char.Humanoid
		local stamValue = plr.Backpack.MovementValues.StaminaValue
		----
		if args[1] == "Running" then
			print("player is running")
			while args[2] do
				stamValue.Value -= 1
				if stamValue.Value == 1 then
					return true
				end
				task.wait()
			end
			return true
		elseif args[1] == "Not Running" then
			print("player is not running")
			while not args[2] do
				stamValue.Value += 1
				if stamValue.Value == 99 then
					return true
				end
				if stamValue.Value == 100  then
					break
				end
				task.wait()
			end
			return true
		end
	end
end

the script isnt finished yet and it cropped out the parts that are not relevant to the topic :grinning:

help and feedback is greatly appriciated. Thank you for your time!

Just some questions: Are there any errors, and what are you using to send the messages between the client and server?
Also, where are the scripts and stuff placed?

1 Like

no errors, and im using a remote function. I wanted to have the client communicate to the server and the server to return something back simply for client sided effects (field of view changing, camera bobbing, basically just eye candy)

You can clamp the value with math.clamp.

How does math.clamp work? ive never heard of it

image
(From the DevHub API page for math)

You use it like this:
math.clamp("the number you want to be clamped/limited", "a lower limit", "a high limit")

So basically, if I did this:
math.clamp(5,1,4)
it would return:
4

So in your case, instead of stopping the stamina when hits 1, which was the problem, each interval, you can set it to it’s previous value whatever, clamped to 0 and the maximum stamina.

1 Like

so i would write something like this?

stamValue.Value = math.clamp(stamValue.Value,0,100)?

stamValue.Value = math.clamp(stamValue.Value-1,0,100) would be what you want

1 Like

This worked! thank you so much!

1 Like

I would do something like this :

local uip = game:GetService('UserInputService')
local isRunning = false
local stamina = 100

uip.InputBegan:Connect(function(key)
      if key.KeyCode == Enum.KeyCode.LeftShift then
            isRunning = true
      end
end)
uip.InputEnded:Connect(function(key)
      if key.KeyCode == Enum.KeyCode.LeftShift then
            isRunning = false
      end
end)

while wait(x) do
      -- do what you want your character to do and do stamina -= 1 (its example of course)
end

It basically make Left Shift a button required to press down, check is stamina on enough level and decrease the stamina by value of yours

1 Like