Script not detecting boolean value being changed

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)
2 Likes

How are you altering the property and from where?

Change values on the server if you’re detecting changes locally, the opposite as well.

1 Like

There’s a lot of variables here, so it’s a bit hard to tell which one exactly you’re having trouble with. I think the BoolValue Hunger?

Through both of those scripts, you’re relying on the Server to detect changes made to variables by the Client, which–with FilteringEnabled active–simply won’t work.

Bear with me through this example:

-- in a LocalScript:
local ChefsPart = Instance.new("Part")
ChefsPart.Parent = workspace
ChefsPart.Name = "ChefsPart"
-- then in a Script:
workspace:WaitForChild("ChefsPart")

To prevent exploits and close up huge vulnerabilities, the client cannot, by default, make changes to the Server that replicate to other clients and the server itself. This means that, in the above scripts, the client would be able to see ChefsPart but the server script wouldn’t; thus, when it calls WaitForChild("ChefsPart"), that statement would yield forever, because the part simply wouldn’t exist on the Server. The same goes for changing values of objects–they won’t replicate.

So, the only solution to your code is to be setting / updating the values on the Server. It looks like your Gui updates when the player is eating–you could change your scripts up such that the Server is updating the value every n seconds and the Client is listening for those changes, and then updating the UI accordingly. From experience, that’s how I usually handle these things.

Side-note: remember that FilteringEnabled exists for a reason! It can be a bit of a pain and a nuisance, having to work around replication issues, but trust me: it’s better to tediously work around this than it is to have exploiters running amok, able to change whatever they wish and having those changes replicate.

Also see:
FilteringEnabled
RemoteFunctions & RemoteEvents

2 Likes

I didn’t read through the code too much, but try hunger.Changed.

There’s a lot of variables here, so it’s a bit hard to tell which one exactly you’re having trouble with. I think the BoolValue Hunger?

Yes, that’s right.
I tried making a remote event that fires if hungerAmount is less than 1 and makes a server script change the hunger value instead of the localscript, and for some reason it still doesn’t work.

--Server Script

local hungerUpdate = game.ReplicatedStorage:WaitForChild("HungerUpdate")

local function updateHunger(Player, hunger, hungerAmount)
	hunger = true
	print("Hunger changed to true")
	print(hungerAmount)
	print(hunger)
end

hungerUpdate.OnServerEvent:Connect(updateHunger)
1 Like

What isn’t working there exactly? Is the event not being called, or is it printing the wrong values?

Either way, you should still be making changes from the Server just because it’s safer. A client could exploit and send false values for hunger & hungerAmount.

1 Like

Sorry, the problem is that even though the print statements are showing the correct values, but it’s not actually doing what it’s supposed to: Let the player eat the food.

RE Making changes from server: I think I’m already doing that, but am I missing something?

It could be that you are looking at the value in the script. Try using the container “data” then searching for the change with the object and its value property

Thank you, I’m away for a bit, so I’ll try that when I get back.

No problem man. I have had similar problems so I’m glad to help. Stay safe!

1 Like

After working on this for a while and the input I’ve gotten here, I can’t seem to find a fix so I just think I need to start fresh and rewrite my script from scratch.
I really appreciate everyone’s help and I’ll keep you updated as things progress with my new code.

I’m back to update that I found a solution!
I realized I need to use bindable functions to detect if the player is full. I did end up rewriting my script and this is what I did:
I put a serverscript inside of ServerScriptService and another script inside of the food. When player clicks on food, it sends a bindablefunction to the serverscript that checks if the player is full or not. If not, then it sends a remoteevent to a localscript inside of the player’s hungerbar gui to increase it.
So, it’s working perfectly now and your suggestions made a big difference. If anyone is interested in a further explanation feel free to DM me. Thanks again!

1 Like