IntValues not changing from LocalScripts

I have a client-sided hunger and thirst mechanic in a game I’m working on. However, when I tested out this code, none of the values had even modified, but the prints have fired? I don’t understand what I’m doing wrong, as I have done this like this before.

local LP = game.Players.LocalPlayer

local Hunger = LP:WaitForChild("Hunger")
local Thirst = LP:WaitForChild("Hydration")
local Energy = LP:WaitForChild("Energy")

function depleteHunger()
	while wait(2.5) do
		if Hunger.Value > 0 then
			Hunger.Value = Hunger.Value - 1
			print("fired")
		end
	end
end

function depleteThirst()
	while wait(5) do
		if Thirst.Value > 0 then
			Thirst.Value = Thirst.Value - 1
			print("does thing")
		end
	end
end

spawn(depleteThirst)
spawn(depleteHunger)

Ur changing the value of variable, not the actual value of IntValue. To change the value u need to write the whole path like this:

game.Players.LocalPlayer:WaitForChild("Hunger").Value = game.Players.LocalPlayer:WaitForChild("Hunger").Value - 1

I believe its because you’re doing it locally. Try using RemoteEvents.
Example:
Client Script:

local replicatedStorage = game:GetService("ReplicatedStorage")

local remoteEvent = replicatedStorage.RemoteEvent

function incrementValue(Object, Value)
    remoteEvent:FireServer(Object, Value)
end
incrementValue(Instance.new("IntValue", player), 5)

Server Script:

local replicatedStorage = game:GetService("ReplicatedStorage")

local remoteEvent = replicatedStorage.RemoteEvent

remoteEvent.OnServerEvent:Connect(function(player, obj, val)
    obj.Value = val
    print(player.Name .. " fired the RemoteEvent. Changed " .. obj.Name .. "'s value to " .. val .. ".")
end)
1 Like

The problem is that you’re not checking if the value is bigger than 0 after you subtract a certain amount from it, I just added 3 values inside of my Local script placed it inside of the character scripts and it works fine here’s the script if you want to check it out.

Script

local LP = script
local Hunger = LP:WaitForChild("Hunger")
local Thirst = LP:WaitForChild("Hydration")
local Energy = LP:WaitForChild("Energy")

function depleteHunger()
	while wait(2.5) do
		if Hunger.Value - 1 > 0 then
			Hunger.Value = Hunger.Value - 1
			print("fired")
		end
	end
end

function depleteThirst()
	while wait(5) do
		if Thirst.Value - 1 > 0 then
			Thirst.Value = Thirst.Value - 1
			print("does thing")
		end
	end
end

spawn(depleteThirst)
spawn(depleteHunger)

Hope this helped.

About waitforchild ur right, u dont need to do that twice, but about -= 1, I know u can do that in python but I am pretty sure u cant use that shortcut in lua

It’s supposed to work client-sided. The value doesn’t change, however.

Again this is not correct. You do realise tables and Instances are passed by reference and not copied?
In fact doing this is actually redundant.

local a = {}
local b = a
b.a = 'c'
print(a.b) --> c

This works, this is LuaU (Roblox Lua), not Vanilla Lua,