Thank you! Sorry for slow reply, that script worked great!!
Unfortunately, it looks like this has caused another issue:
The playerIsHungry value is only changing when the player eats (clicks) food.
Instead, I only want playerIsHungry value to change when ‘player hunger bar’ reaches max.
I’ve been trouble-shooting and wrote some new script but it’s causing I’m back to the same problem, it’s not changing the playerIsHungry value again. Ugh.
Here’s the code:
Localscript (Manages the player’s hungerBar size and hungerAmount)
local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:wait()
local Human = Character:WaitForChild("Humanoid")
local hungerFrameScale = script.Parent.Size.X.Scale
local hungerFrame = script.Parent
local foodClicked = game.ReplicatedStorage:WaitForChild("FoodClicked")
local data = Player:WaitForChild("Data")
local depletion = data:WaitForChild("Depletion")
local hungerAmount = data:WaitForChild("HungerAmount")
local hunger = data:WaitForChild("Hunger")
local remoteHungerEvent = game.ReplicatedStorage:WaitForChild("RemoteHungerEvent")
local function increaseFood(Player)
hungerAmount.Value = hungerFrameScale + .1
hungerFrame.Size = UDim2.new(hungerFrameScale+.1,0,1,0)
hungerFrameScale = script.Parent.Size.X.Scale
if hungerFrameScale > 1 then
hungerFrameScale = 1
hungerFrame.Size = UDim2.new(1,0,1,0)
end
hungerAmount.Value = hungerFrameScale
print("Food eaten")
end
remoteHungerEvent.OnClientEvent:Connect(increaseFood)
while true do
if hungerFrameScale > 0 then
wait(5)
hungerFrame.Size = UDim2.new(hungerFrameScale-depletion.Value,0,1,0)
hungerFrameScale = script.Parent.Size.X.Scale
hungerAmount.Value = hungerFrameScale
print("Hunger loop")
else
wait(5)
Player.Character.Humanoid:TakeDamage(5)
print("Death loop")
end
end
Foodscript (Changed, manages the plant and fires the hungerEvent to hungerManager when eaten)
local foodClicked = game.ReplicatedStorage:WaitForChild("FoodClicked")
local eaten = script.Parent.Eaten
local clickDetector = script.Parent.ClickDetector
local hungerEvent = game.ReplicatedStorage:WaitForChild("HungerEvent")
function makeFoodVisible(gameWantsFoodVisible)
if gameWantsFoodVisible then
script.Parent.Transparency = 0
clickDetector.MaxActivationDistance = 10;
else
script.Parent.Transparency = 1;
clickDetector.MaxActivationDistance = 0;
end
end
function FoodClicked(Player)
local playerIsHungry = Player.Data.Hunger.Value
local hungerAmount = Player.Data.HungerAmount.Value
local thisFoodEaten = eaten.Value
if not thisFoodEaten and playerIsHungry then -- 'if not thisFoodEaten' is the same as saying if 'thisFoodEaten == false'
print("The player is able to eat the food!")
hungerEvent:Fire(playerIsHungry, Player, hungerAmount)
thisFoodEaten = true
makeFoodVisible(false)
wait(2)
thisFoodEaten = false
makeFoodVisible(true)
end
end
clickDetector.MouseClick:Connect(FoodClicked)
HungerManager (Determines whether the player is hungry or not.)
local hungerEvent = game.ReplicatedStorage:WaitForChild("HungerEvent")
local remoteHungerEvent = game.ReplicatedStorage:WaitForChild("RemoteHungerEvent")
hungerEvent.Event:Connect(function(playerIsHungry, Player, hungerAmount)
if hungerAmount == 1 then
playerIsHungry = false
print(playerIsHungry)
else
playerIsHungry = true
print(playerIsHungry)
remoteHungerEvent:FireClient(Player)
end
end)
Kinda stumped and not sure where to go from here. I know I went a little of track here so if that’s a problem I still have the script you sent me, and we can work off of just that if you’d like to. TYIA