Issue w/ Hunger Bar & System

  1. Goal: An apple will bring my hunger up, and once I eat it it will be removed from my inventory.

  2. Issue: My hunger bar works fine until I eat the food. The apple increases my hunger, but it is not removed from my inventory + if I select & unselect the apple it moves me around and brings me up.

Here is my script:

Placed inside the tool I made for the food, local script [ this is most likely the issuse as everything else is working]* :

local Eaten = false

script.Parent.Activated:Connect(function()
	if Eaten then
		return
	end
	
	if Eaten == true then
		wait(1)

		game.ReplicatedStorage.Eat:FireServer()	
		script.Parent:Destroy()
	end
	
		

end)

Placed in Starter Script Storage for the number value : [this is working, but I’m putting this here just in case]

local DECREASE_FREQUENCY = 10
local DECREASE_AMOUNT = 2
local INCREASE_AMOUNT = 8

game.Players.PlayerAdded:Connect(function(Player)
	
	local Hunger = Instance.new("IntValue", Player) 
	Hunger.Name = "Hunger"
	Hunger.Value = 100 -- how much hunger yo will have
	
	Player.CharacterAdded:Connect(function()
		Hunger.Value = 100
	end)
	
	while wait(DECREASE_FREQUENCY) do
		-- decrease value
		
		if Hunger.Value <= 0 then
			Player.Character:BreakJoints()
		else
			Hunger.Value -= DECREASE_AMOUNT
		end
		
		
	end
	
end)

game.ReplicatedStorage.Eat.OnServerEvent:Connect(function(Player)
	Player.Hunger.Value += INCREASE_AMOUNT
end)

Placed in the screen GUI to tween it, local script [this is working, but I’m putting this here just in case]

local Player = game.Players.LocalPlayer 
local Hunger = Player:WaitForChild("Hunger") -- waiting for the hunger value to exist 
local GUI = script.Parent 
local BarExterior = GUI.Bar
local BarInterior = BarExterior.Bar

local function UpdateHunger()
	-- update the hunger
	local CurrentValue = Hunger.Value
	local Formula = math.clamp(CurrentValue/100,0,1) -- making sure that 0 is min and 100 is max
	BarInterior:TweenSize(UDim2.new(Formula, 0, 1, 0), Enum.EasingDirection.Out, Enum.EasingStyle.Linear, 0.15, true)
end
	
UpdateHunger()
Hunger.Changed:Connect((function()
	UpdateHunger() -- call function
end))

Thank you so much!!

1 Like

ok, just to say something, i noticed you were using an int value, personally i find those unreliable and sometimes they flat out decide not to work. i would use a module script but it’s up to you. kinda unrelated but anyways, sorry.

script.Parent.Activated:Connect(function()
	if Eaten then
		return
	end
	Eaten = true -- What you want
	
	--if Eaten == true then -- issue line
		wait(1)

		game.ReplicatedStorage.Eat:FireServer()	
		script.Parent:Destroy()
	--end

You start the function by checking if the apple is eaten and returning, then you check again if it is eaten. You need to remove the second if check, currently it is programmed to always do nothing. You probably meant to set Eaten = true at the start of the function.

Here

if Eaten then
		return
	end
	
	if Eaten == true then

This makes no sense you probably missed something maybe you meant if Eaten == false

try this

local Eaten = false

script.Parent.Activated:Connect(function()
	if Eaten then
		return
	end
    Eaten = true
    wait(1)

	game.ReplicatedStorage.Eat:FireServer()	
	script.Parent:Destroy() 
	
		

end)