-
Goal: An apple will bring my hunger up, and once I eat it it will be removed from my inventory.
-
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!!