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