Attempt to index nil with 'GetAttribute'

I use attributes to handle the stamina and hunger bars in my game, and this showed up for one of my friends who was helping me test;

It’s referring to the stamina entry in line 56, located in this function:

function updatestats()
	local hp = math.clamp(hum.Health / hum.MaxHealth, 0, 1)
	local stm = math.clamp(charvalues:GetAttribute("STM") / 100, 0,1)
	local hgr = math.clamp(charvalues:GetAttribute("HGR") / 100, 0,1)
	pstats.HealthBar.HealthAmount.Size = UDim2.fromScale(hp, 1)
	pstats.StaminaBar.StaminaAmount.Size = UDim2.fromScale(stm, 1)
	pstats.HungerBar.HungerAmount.Size = UDim2.fromScale(hgr, 1)
end

Only they get this error though, not me. However, it could vary for other players. I don’t really know how I’d fix it, considering it should work as is.

1 Like

“charvalues” doesn’t exist in your context. The function/script may have been called before they loaded, or the directory isn’t a real one.

It should be existing since there’s a function that does that again when you respawn.

plr.CharacterAdded:Connect(function(newCharacter)
	hrp = newCharacter:WaitForChild("HumanoidRootPart", 10)
	plr = plr
	char = newCharacter
	hum = char:WaitForChild("Humanoid")
	coreui = plr.PlayerGui:WaitForChild("CoreUI")
	uimain = coreui:WaitForChild("Main")
	pstats = uimain:WaitForChild("PlayerStatus")
	charvalues = char:FindFirstChild("CharacterValues")
	pstats.PlayerImage.PlayerImage.Image = plrs:GetUserThumbnailAsync(plr.UserId, Enum.ThumbnailType.HeadShot, Enum.ThumbnailSize.Size420x420)
	pstats.Money.Amount.Text = comma_value(plr.PlayerData.orth.Value)
end)

function updatestats()
	local hp = math.clamp(hum.Health / hum.MaxHealth, 0, 1)
	local stm = math.clamp(charvalues:GetAttribute("STM") / 100, 0,1)
	local hgr = math.clamp(charvalues:GetAttribute("HGR") / 100, 0,1)
	pstats.HealthBar.HealthAmount.Size = UDim2.fromScale(hp, 1)
	pstats.StaminaBar.StaminaAmount.Size = UDim2.fromScale(stm, 1)
	pstats.HungerBar.HungerAmount.Size = UDim2.fromScale(hgr, 1)
end

Well, where do you define the chat values other than the respawn script? Instead of using FindFirstChild, just use WaitForChild instead.

1 Like

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