Writing code for a survival game.
Print statement says that the hunger value is set to true but the food script doesn’t seem recognize it.
Any input would be great.
Thanks!
--Local script
local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:wait()
local Humanoid = Character:WaitForChild("Humanoid")
local hungerFrameScale = script.Parent.Size.X.Scale
local hungerFrame = script.Parent
local data = Player:WaitForChild("Data")
local depletion = data:WaitForChild("Depletion")
local hungerAmount = data:WaitForChild("HungerAmount")
local hunger = data:WaitForChild("Hunger")
hunger:GetPropertyChangedSignal("Value"):Connect(function()
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)
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
if hungerAmount.Value < 1 then
hunger.Value = true
print("Hunger changed to true")
print(hungerAmount.Value)
print(hunger.Value)
end
print("Hunger loop")
else
wait(5)
Character.Humanoid:TakeDamage(5)
print("Death loop")
end
end
--Food Script
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
local hungerAmount = Player.Data.HungerAmount
local thisFoodEaten = eaten
if not thisFoodEaten.Value and playerIsHungry.Value then
print("The player is able to eat the food!")
hungerEvent:Fire(playerIsHungry, Player, hungerAmount)
thisFoodEaten = true
makeFoodVisible(false)
if hungerAmount.Value == 1 then
hungerEvent:Fire(playerIsHungry, Player, hungerAmount)
print("Player is no longer hungry.")
else
hungerAmount.Value += .1;
end
wait(2)
thisFoodEaten = false
makeFoodVisible(true)
end
end
clickDetector.MouseClick:Connect(FoodClicked)