Attempt to index nil with 'Changed'

Hello! Im making a stamina system and this local script listens to whenever the value of the stamina IntValue changes. I don’t know why this is happening as everything is spelt right and is in the right place. If you could help me, I would really appreciate it.

Thank you.

local plr = game.Players.LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait()
local stamina = char:FindFirstChild("Stamina")
local maxstamina = 100
local ts = game:GetService("TweenService")
local ti = TweenInfo.new(0.1, Enum.EasingStyle.Quint, Enum.EasingDirection.In)

stamina.Changed:Connect(function()
	local percent = plr.Character:FindFirstChild("Stamina").Value / maxstamina * 0.35
	ts:Create(script.Parent, ti, {Size = UDim2.new(percent, 0, 0.064, 0)})
	--script.Parent.Size = UDim2.new(percent, 0, 0.064, 0)
	--the 0.064 value is the Y value of GreenBar
end)

probably because the script runs before the Stamina value gets created, try replacing the FindFirstChild in the stamina variable with WaitForChild

1 Like

thanks :sweat_smile: i cant believe i didnt think of that

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