How can I reset my stamina system after my player dies?

Hello developers!

Today I have a question of about resetting my stamina value after my player dies.

So when I swing my weapon in game, I lose stamina. However, the bug is that when I die and respawn, the stamina was at the same value as it was before I died (Or regenerating). I just need a simple fix that can ensure that my value goes back to 100. I know this may be a simple fix, but I’m still fairly new to coding.

I’ll leave the code below:

local KillEvent = script.Parent.Parent.Parent:WaitForChild(“Events”).KillEvent
local Tool = script.Parent.Parent.Parent
local Attack = Tool.Animations:WaitForChild(“Attack1”)
local Attack2 = Tool.Animations:WaitForChild(“Attack2”)
local Player = game.Players.LocalPlayer
local Stamina = script.Parent.Parent.Parent:WaitForChild(“Values”).Stamina
local LastAttack = tick()
local db = false
local IsHit = false
local swinging = false

local function RegenerateStamina()
while true do
if not swinging and Stamina.Value < 100 then
Stamina.Value = math.min(Stamina.Value + 10, 100)
print(Stamina.Value)
end
wait(1)
end
end

spawn(RegenerateStamina)

Tool.Activated:Connect(function()
if db then return end
db = true
swinging = true

if Stamina.Value <= -10 then
	db = false
	swinging = false
	return
end
print(Stamina.Value)

if tick() - LastAttack < 1 then
	local Animation = Player.Character.Humanoid:LoadAnimation(Attack2)
	IsHit = true
	Animation:Play()
	Tool:WaitForChild("Blade").Touched:Connect(function(hit)
		if hit.Parent:FindFirstChild("Humanoid") and IsHit == true then
			KillEvent:FireServer()
			IsHit = false
		end
	end)
	Stamina.Value = Stamina.Value - 10
else
	local Animation = Player.Character.Humanoid:LoadAnimation(Attack)
	Animation:Play()
	LastAttack = tick()
	IsHit = true
	Tool:WaitForChild("Blade").Touched:Connect(function(hit)
		if hit.Parent:FindFirstChild("Humanoid") and IsHit == true then
			KillEvent:FireServer()
			IsHit = false
		end
	end)
	Stamina.Value = Stamina.Value - 10
end

wait(0.5)
db = false
swinging = false

end)

Thanks for your help!
Troohook

1 Like

If im correct, you using a server script inside of a tool. (you havent added that information)

If a player dies there is a new character, therefore a new tool. Maybe set the stamina.value to 0 at the beggining of the script. This way each time the tool ‘resets’ the stamina is set to 0.

edit: instead of 0 i meant to the max
edit2: sorry i didnt realise it was a local

1 Like

This has to be a localscript because of local Player = game.Players.LocalPlayer. Though I don’t understand why the stamina wouldn’t be reset in the first place if a new tool is cloned when the player respawns.

@Troohook Can you confirm the script is in a tool and not inside the player, and that a new copy of the tool is given to the player on respawn (as opposed to the tool dropping on the ground and getting picked up again or something)? Ideally, do you have a small testplace to show the issue?

3 Likes

the stamina value object seems to be on a pretty odd place:

local Stamina = script.Parent.Parent.Parent:WaitForChild(“Values”).Stamina

it seems to be outside of the character or tool so thats why it doesnt reset I think.
Maybe it would be nice if troohook told us where this stamina value exactly is in the game.

Try adding this at the very bottom of your script (outside of any function or event/connection):

-- Using "Once" will disconnect automatically after the Tool is equipped for the first time
-- It should reconnect automatically when (or if, if something else is blocking the Tool's scripts from resetting) the Tool's scripts reset
Tool.Equipped:Once(function()
	Stamina.Value = 100
end)

Hi, Thanks for the reply! The stamina is an IntValue that is inside a folder within the tool.

image

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