Value won't update

Hello, I’m trying to create a stamina system along with other stats, but the stamina levels won’t update. I don’t really script anything past basic level but I know how to use the Naturestore module so I have been using that as a datastore and have been trying to get all these stats to update live in the playerdata as much as I can.
At first it ran on a lot of “while true do” segments but I removed all of them but the main one and everything works still so I figured that wasn’t the problem.
A little background about how the script is supposed to function with other scripts. The “Leaelly” is the character I am using and has a localscript in it that works the controls, when it goes into a sprinting mode from the localscript, a remote event is firing to set a bool value to true(or false if they stop sprinting) that this script is supposed to read. All of the previous works fine, but when it comes to the code below it seems to do nothing, no errors on the output, doesn’t print the quoted text in the output either.

while true do
--Energy Regen
		Energy = Data.LeaellyEnergy.Value
		if Energy < 100 and Sprinting == false then
			NatureStore:IncrementData(plr.UserId, "LeaellyEnergy", 1)
			repeat until Energy == 100
		if Sprinting == true then
			NatureStore:IncrementData(plr.UserId, "LeaellyEnergy", -1)
		elseif Sprinting == true and Data.LeaellyEnergy.Value <= 0 then
			Sprinting = false
			elseif Sprinting == false then 

I do have all the appropriate globals/locals in a directory at the top of the script. I’m at quite a loss, been trying to fix this for hours. That last elseif statement was another attempt at fixing the problem, but I never expected it to do anything either.

Not gonna change anything, but I would change while true do to while task.wait() do and I would change the wait() to task.wait()

Why do you need a data store for checking the stamina?

Also, are there any errors in the output?

1 Like

The stamina levels are one of the values saved in the datastore so someone can’t just rejoin when they are low on stamina to get a new fill and get out of a situation that would be dangerous under normal circumstances. I figured updating the values live would be most efficient even though the datastore still only actually saves the data at a certain interval of time.

Also no errors at all in the output.

I’ll look into changing to task.wait() because I currently have no idea what it does, but I found the announcement for that just a moment ago.

Changing it actually does do something.
while true do is optimized to function as a constant loop. The compiled code will be inconsequentially shorter and faster. Changing it to while task.wait() do disables this optimization because the compiler can’t guarantee that the result of task.wait() will be truthy.

1 Like

Yup I agree, when I said it won’t do anything I meant it won’t be the solution.

1 Like

i think you should try using

while wait(2) do
--the rest

instead of while true do because the code runs again every 2 secods and when stuff changes
it will realize it when doing it later
idk how to explain this but yes.

I believe I have found my own solution by rewriting the script to function in a different way to recieve the desired outcome. Thank you for all your responses.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.