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