How would I go about making a stamina system?

Hmmm

You don’t really need to assign local = data inside the function

Should i make the values nil before the thread

OOPS, didn’t see that, i forgot to delete

No, because if you change the value of a certain item inside the array to nil and try to print the table, then it would not show up since it’s a nil.

The error still occurs, what i meant is

local data = {}
local canRegen = nil
local run = nil
local canSpawn = nil

Oh, I forgot to mention that you also need to add the player’s name inside the array.

data[plr.Name] = {}
-- Then you can add the canRegen that's why it's a nil index because the name of the player is not added yet.

data[plr.Name]["canRegen"] = true
data[plr.Name]["canSpawn"] = true
data[plr.Name]["run"] = false

Now it works better than it ever has, but if i punch multiple times or run multiple times, it spawns a function no matter what 2 seconds after the event.

So if i punch and then punch 1 second after it will first start regenerating after 2 seconds and then 1 second. How can i fix this

Can you show me your new script I can’t really check what’s causing the problem.

Ofcourse, well there is no problem cause I haven’t made any logic or tick() for that, and even if you explain how I should do that I would probably be lost. I hate tick() haha

local RS = game:GetService("ReplicatedStorage")
local STAM_REMOTE = RS:WaitForChild("Remotes"):WaitForChild("Stamina")
local STAM_BIND = RS:WaitForChild("Remotes"):WaitForChild("StaminaBind")

local data = {}

STAM_REMOTE.OnServerEvent:Connect(function(plr, action)
	
	data[plr.Name] = {}
	
	local stamina = plr.Character:WaitForChild("Stamina")
	
	if action == "startedRun" then
		
		data[plr.Name]["canRegen"] = false
		data[plr.Name]["run"] = true
		
		while data[plr.Name]["run"] == true do
			
			if stamina.Value >= 1 then
				
				stamina.Value -= 1
				
				wait(0.05)
				
				if stamina.Value == 0 then
					
					data[plr.Name]["run"] = false
					
				end
				
			else
				
				stamina.Value = 0
				break
				
			end
			
			print(stamina.Value)
			
		end
		
	elseif action == "stoppedRun" then
		
		data[plr.Name]["run"] = false
		
		wait(2)
		
		data[plr.Name]["canRegen"] = true
		
		
	elseif action == "jump" then
		
		if stamina.Value >= 10 then

			stamina.Value -= 10


		else

			stamina = 0

		end
		
		print(stamina.Value)

	end
	
	wait(2)
	
	data[plr.Name]["canSpawn"] = false
	data[plr.Name]["canRegen"] = true
	data[plr.Name]["canSpawn"] = true
	
	if data[plr.Name]["canSpawn"] == true then
		
		spawn(function()
			
			while data[plr.Name]["canRegen"] == true and data[plr.Name]["canSpawn"] == true do
				
				if stamina.Value < 100 then
				
					stamina.Value += 1
					
				elseif stamina == 100 then
					
					data[plr.Name]["canRegen"] = false
					break
					
				end
				
				print(stamina.Value)
				wait(0.1)
				
			end
			
			
			
		end)
		
	end
	
end)

STAM_BIND.Event:Connect(function(plr, action)
	
	local stamina = plr.Character:WaitForChild("Stamina")
	
	data[plr.Name] = {}
	
	if action == "punch" then
		
		if stamina.Value >= 5 then
			
			stamina.Value -= 5
			
			
		else
			
			stamina = 0
			
		end
		
		print(stamina.Value.. " punch")
		
	end
	
	data[plr.Name]["canSpawn"] = false
	
	wait(2)

	data[plr.Name]["canRegen"] = true
	data[plr.Name]["canSpawn"] = true

	if data[plr.Name]["canSpawn"] == true then

		spawn(function()

			while data[plr.Name]["canRegen"] == true and data[plr.Name]["canSpawn"] == true do
				
				wait(0.1)
				
				if stamina.Value < 100 then

					stamina.Value += 1

				else

					data[plr.Name]["canRegen"] = false
					break

				end
				
				print(stamina.Value)

			end



		end)

	end
		
end)
1 Like

The problem with your script is that after the condition have been fulfilled you have set that both canRegen and canSpawn would always be true meaning once you use the action punch after 2 seconds canRegen and canSpawn is true, same with the remote event.

That is for the script to stop the other spawned function

@Clickzy @MakerDoe hi there, any updates? What are we onto now?

Okay so I’ve basically made the script work except for one thing. I will show you with a video. It will be easier to understand.

Nevermind, can’t even open Roblox studio, servers are down.

1 Like

I would really highly recommend using tick() or os.time() here, the example would be when the player join’s, a while loop is always looping checking the new time and the old time is greater than 2 seconds if so the player’s stamina would increase, it’s not that difficult really, instead of canRegen = true, it would be = tick().

I made a little experiment, and it works perfectly fine:

local RS = game:GetService("ReplicatedStorage")
local STAM_REMOTE = RS:WaitForChild("Remotes"):WaitForChild("Stamina")
local STAM_BIND = RS:WaitForChild("Remotes"):WaitForChild("StaminaBind")

local data = {}

STAM_REMOTE.OnServerEvent:Connect(function(plr, action)
	local stamina = plr.Character:WaitForChild("Stamina")

	if action == "startedRun" then
		data[plr.Name]["run"] = true

		spawn(function()
			while data[plr.Name]["run"] == true and stamina.Value > 1 and wait(0.05) do
				stamina.Value -= 1
				data[plr.Name]["canRegen"] = tick()

				if stamina.Value <= 0 then
					stamina.Value = 0
					data[plr.Name]["run"] = false
				end
			end
		end)
	elseif action == "stoppedRun" then
		data[plr.Name]["run"] = false
	end
end)

game.Players.PlayerAdded:Connect(function(player)
	local stamina
	
	player.CharacterAdded:Connect(function(character)
		stamina = Instance.new("IntValue", character)
		stamina.Name = "Stamina"
		stamina.Value = 100
	end)
	if not data[player.Name] then
		data[player.Name] = {}
		data[player.Name]["canRegen"] = tick()
		data[player.Name]["run"] = false
	end
	
	local char = player.Character or player.CharacterAdded:Wait()
	
	while wait(0.05) do
		if (tick() - data[player.Name]["canRegen"]) > 2 and stamina.Value < 100 then
			stamina.Value += 1
			
			if stamina.Value >= 100 then
				stamina.Value = 100
			end
		end
	end
end)
1 Like

I don’t like being feeded code so I will analyse the code and see how it works!

1 Like

Holy! I didn’t even think of this, I thought I would have to check for it in the same thread! Every event just changes canregen to the current tick, and if the tick is above 2 seconds it begins to generate. That’s really clever! Thanks alot. You’ve earned my respect.

1 Like