Why won't this script work when the player's character is added?

Hello!
I have a script that won’t put the player’s hunger to max when they respawn. Please Help!

The script.

local Humanoid
local Char
local MaxHunger = 100
local HungerValue

game.Players.PlayerAdded:Connect(function(player)
	local HungerVal = Instance.new("IntValue", player)
	HungerVal.Name = "HungerValue"
	HungerVal.Parent = player
	HungerVal.Value = MaxHunger
	HungerValue = player.HungerValue

	player.CharacterAdded:Connect(function(char)
		HungerValue.Value = MaxHunger
		print("Hunger Max")
	end)
end)

You could do something like this:

if I do the instance.new in the character added l will have multiple. But I will try!

Yeh I’m not sure maybe you can have it outside of the instance thingy?

ah idk why but the script doesn’t work its no making the value 100 for some reason

Can you provide the entire script, so we can help? Also you named your value as “HungerVal”, but then tried to get “HungerValue”. It must match.

yes,

local DotHunger = HungerFrame.DotHunger
local Circle = HungerFrame:WaitForChild("Circle")
local HungerShow = HungerFrame.HungerShow
local MaxHunger = 100
local HungerValue = Player:WaitForChild("HungerValue")
local HungerColor
local HungerChange

HungerValue:GetPropertyChangedSignal("Value"):Connect(function()
	HungerColor = Color3.fromRGB(218, 133, 65):Lerp(Color3.fromRGB(13, 105, 172), HungerValue.Value/MaxHunger)
	HungerChange = HungerValue.Value/MaxHunger
	
	DotHunger:TweenSize(UDim2.new(HungerChange, 0, 1, 0), "In", "Quad", 1)
	DotHunger.BackgroundColor3 = HungerColor

	HungerShow.Text = HungerValue.Value.."/"..MaxHunger
	Circle.ImageColor3 = HungerColor
end)

while wait(3) do
	if HungerValue.Value >= 1 then
		HungerValue.Value = HungerValue.Value - 1
	end

	if HungerValue.Value == 0 then
		repeat wait(1)
			Humanoid.Health = Humanoid.Health - 5
		until Humanoid.Health == 0 or HungerValue.Value < 0
	end
end

Player.CharacterAdded:Connect(function()
	HungerValue.Value = MaxHunger
end)

the local script

I Just added the character added to see if it would work

 local HungerVal = Instance.new("IntValue", player) -- Your Hunger Value
	HungerVal.Name = "HungerValue"
	HungerVal.Value = 0


        HungerVal.Value = MaxHunger

oh, I will try that! soon :P!!

Oops I messed up, it should be something like this:

local MaxHunger = 100
local HungerValue

game.Players.PlayerAdded:Connect(function(player)

		local HungerVal = Instance.new("IntValue", player)
		HungerVal.Name = "HungerValue"
		HungerVal.Value = 0 -- Starting at 0

	      player.CharacterAdded:Connect(function(char)

			char:WaitForChild("Humanoid").Died:Connect(function() -- When Humanoid Dies
			print(player.Name.."Died")

			player.CharacterAdded:Wait() -- When player spawns again 
			HungerVal.Value = MaxHunger -- Value is set to 100
			print("Hunger Max")
			end)
		end)
	end)

Why set HungerValue? If you’re defining it outside of the characteradded event it’d get changed every time the player joins. Essentially, only the player that joined most recently would have their hunger updated.

Yes that is my apologies. It’s not necessary to set the HungerValue as it’s already defined.

I also forgot that “characterAdded” function should be moved outside of the Instance, as it will keep duplicating every time the character is added. Thus it will only happen for the most recent player as well.

I’ll update the code :+1:t2:

local MaxHunger = 100
game.Players.PlayerAdded:Connect(function(player)
	local Hunger = Instance.new("IntValue", player)
	Hunger.Name = "HungerValue"
	Hunger.Value = MaxHunger
	player.CharacterAdded:Connect(function(character)
		Hunger.Value = MaxHunger
		print("Gave '"..player.Name.."' Max Hunger.")
	end)
end)

I already figured it out! sry for not replying, but I subtracted hunger value by hunger value then I added max hunger.