"Attempt to index nil with StaminaChanged"

I get the error ServerScriptService.Stamina:12: attempt to index nil with 'StaminaChanged' when trying to run my code

local staminaBar = game.ServerStorage.CharacterBar.MainStamina.StaminaBar
local staminaText = game.ServerStorage.CharacterBar.MainStamina.staminaText
local maxStamina = 100
local stamina = 100
local human = game.Players.LocalPlayer


staminaBar:TweenSize(UDim2.new(stamina/maxStamina, 0, staminaBar.Size.Y.Scale, 0), Enum.EasingDirection.Out, Enum.EasingStyle.Sine, 0.1, false)

staminaText.Text = stamina .. "/" .. maxStamina

human.StaminaChanged:Connect(function(currentStamina)

	maxStamina = human.MaxStamina

	staminaBar:TweenSize(UDim2.new(currentStamina/maxStamina, 0, staminaBar.Size.Y.Scale, 0),  Enum.EasingDirection.Out, Enum.EasingStyle.Sine, 0.1, false)

	staminaText.Text = math.round(currentStamina) .. "/" .. maxStamina

end)

(which is a serverscript in serverscriptservice) though the script sets the stamina bar to 100/100

  1. What do you want to achieve? I’d like to create a player event called StaminaChanged

  2. What solutions have you tried so far? I tried making a ModuleScript in ServerScriptService for StaminaChanged but I couldn’t get that to work.

1 Like

You would probably create an attribute called stamina in your player with a script, and use :GetAttributeChangedSignal(“Stamina”)

2 Likes

Actually I would recommend creating the attribute in Humanoid, but not player.

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