Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

while true do loop only cycling one time?

the goal of this script is after 12 hours a player receives a reward, the problem is that the while true do loop only cycles once

local RewardTime = 12*60*60
 
local DataStore = game:GetService("DataStoreService"):GetDataStore("LastLogin")
local Player = script.Parent.Parent.Parent.Parent.Parent
local LastLogin = DataStore:GetAsync(Player.userId) or os.time()
 
while true do
	Seconds = RewardTime-(os.time()-LastLogin);
	Minutes = Seconds/60;
	Hours = Minutes/60;	
	script.Parent.Text = ("Time Until Next Reward: d:d:d"):format(Hours,Minutes%(60),Seconds%(60))
	wait(1)
	DataStore:SetAsync(Player.userId,os.time())
end

Thanks

6 comments

0

Check your output before you post here. If a while true do loop only works once, it errors somewhere.

0

no error output tho

0

It's most likely because you're sending a SET request to data store every one second (which you should not be doing), therefore the request get's throttled. Since every request in data store yields, your loop won't recycle until the request is done throttling (which can be a very long time)

0

Actually, let me change that "most likely" to "most certainly", because that is the problem. I'll write an answer because this can be tricky to explain to someone who isn't that familiar with the limitations of data store.

0

is there a better way to save it without it being in the loop?

0

Don't worry, I'm working on an explanation.

1 answer

(This is not meant to be a full answer; ScriptGuider is apparently working on a good one.) Some problems with your script:

  • This appears to be a LocalScript. DataStoreService says that DataStores are server-side only. (This won't beocme an issue until you try it in an online mode.) To fix this, use RemoteEvents/Functions to allow the client (that should be displaying how much time is left for the next reward) and the server (which is responsible for telling the client how much time is left via a RemoteFunction and updating the DataStores as required)
  • Not that anyone playing would likely realize, but you are penalizing players for staying in your game. If someone stays in for 6 hours, you will have SetAsync the most recent time they were online, meaning they will have to wait another 12 hours before getting the next reward (for a total of 18 hours before awards). Consider changing the "last login" time to "last gave award" time. In terms of the script, this means only updating the key with the time when you have given the award.
  • You must always wrap calls to DataStores with pcall since they may error (the DataStores may be down, for instance). If they fail, you should consider waiting a few seconds (ex maybe 10 seconds) before trying again. Either repeat this indefinitely, or quit after a number of attempts (and tell the user about the failure).
  • Your server only receives a limited number of requests per minute, see here in the manual. (If you take the suggestion in my second bullet point, you will only save the time once every 24h, so that won't be a problem.)
  • You never seem to actually test to see if you should give the player the award (maybe you just didn't post that code). -Your formatting code needs to be "%d:%d:%d" (you missed the '%'). You can make it look prettier by using "d", which makes sure that 2 digits will always display (ex "01" instead of just "1"). ex, you might want "%d:d:d", and if there is 1h, 2 min, 3 sec left, it will show "1:02:03"

Minor: you don't generally need semicolons at the end of a line

This discussion is preserved as it was. New answers, comments, and votes are closed.