Help with disabling and re-enabling a function of a LocalScript

Hello again! I’m having some trouble adding a section of script into this hunger & thirst script. I want it so that when a value in a player’s Leaderstats is changed, the LocalScript detects it and starts the function, and when that value is changed to false, the LocalScript again detects it and pauses the function. I’ve tried some while wait() do functions, but they aren’t working.

Here’s the whole script (photo because the preformatting isn’t working for some reason):


Is there a way to do this? Please help.

Transcribed:

local MainFrame = script.Parent.MainFrame -- start Screenshot_26.png
local Player = game.Players.LocalPlayer

local Active = Instance.new("BoolValue", Player)
Active.Name = "Active"
Active.Value = false

local HealthBar = MainFrame:WaitForChild("HealthFrame"):WaitForChild("HealthBar")
local HungerBar = MainFrame:WaitForChild("HungerFrame"):WaitForChild("HungerBar")
local ThirstBar = MainFrame:WaitForChild("ThirstFrame"):WaitForChild("ThirstBar")

repeat wait() until Player.Character

local Humanoid = Player.Character:WaitForChild("Humanoid")

local MaxHunger = 100
local MaxThirst = 100

local HungerDecreaseRate = 1 -- in seconds
local ThirstDecreaseRate = 1 -- in seconds

if Player:FindFirstChild("HungerVal") then
	HungerValue = Player.HungerVal
	HungerValue.Value = MaxHunger
else
	Instance.new("IntValue", Player).Name = "HungerVal"
	Player.HungerVal.Value = MaxHunger
	HungerValue = Player.HungerVal
end

if Player:FindFirstChild("ThirstVal") then
	ThirstValue = Player.ThirstVal
	ThirstValue.Value = MaxThirst
else -- start Screenshot_27.png
	Instance.new("IntValue", Player).Name = "ThirstVal"
	Player.ThirstVal.Value = MaxThirst
	ThirstValue = Player.ThirstVal
end

HungerValue.Changed:Connect(function()
	HungerBar:TweenSize(UDim2.new(HungerValue.Value/MaxHunger,0,1,0), "Out", "Linear", .2, true)
end)

ThirstValue.Changed:Connect(function()
	ThirstBar:TweenSize(UDim2.new(ThirstValue.Value/MaxThirst,0,1,0), "Out", "Linear", .2, true)
end)

spawn(function()
while wait(HingerDecreaseRate) do
	if HungerValue.Value - 1 >= 0 then
		HungerValue.Value = HungerValue.Value - 1
	end
	if HungerValue.Value == 0 then
		repeat wait(1)
			Humanoid.Health = Humanoid.Health - 5
		until HungerValue.Value > 0 or Humanoid.Health <= 0
	end
end
end)
1 Like

If you want to detect when a player’s leaderstats have changed, I would wait for leaderstats to be created under the player and then find the value you want to listen for, and connect a function to its .Changed event that enables a loop and keeps it open for as long as that value is true or equals some number or whatever.
I would use code like this:

local leaderstats = Player:WaitForChild("leaderstats")
local BoolValue = leaderstats:WaitForChild("BoolValue")

BoolValue.Changed:Connect(function()
	while BoolValue.Value == true do
		-- whatever you want to do
		wait()
	end
end)

Since event connections run in a different thread, you will not have to worry about spawning the function, which is nice.

Also, important note, while do loops do not have to use a wait in the first statement between while and do, they just run for as long as that condition is not false or nil. The wait() function returns the number of seconds that passed after it was done waiting, which is indeed not false or nil. This is the same for repeat until loops as well, except they obviously stop when the condition at the end is not true.

Sorry if you already knew this…

I tried to add that - it unfortunately didn’t work.

Oh, that’s because you are spawning both functions in different threads.
You should put a check for the value in each of the spawn functions as well!

Active.Changed:Connect(function()
	spawn(function()
		while wait(HungerDecreaseRate) and Active.Value == true do
			--...
		end
	end)
	spawn(function()
		while wait(ThirstDecreaseRate) and Active.Value == true do
			--...
		end
	end)
end)
1 Like

Thanks for your help, everything is now working smoothly. :grinning:

1 Like