#Loops and Why They're Dangerous
Loops run a piece of code over and over again, hence their name. Running code uses some processing power. If you make an infinite loop or a really large loop (like a while do loop) and there are no delays, the script will run the same code over and over again an infinite amount of times with no breaks. Your processor will have too many tasks to do and won't be able to handle all the stress it is under.
#Solution
Add a wait, yield or delay of any kind. The reason why your code was working up until the 1 minute mark is because of the second half of your script:
elseif minutes == 0 and seconds <= 59 then
seconds = seconds + 1
_G.timePlayed[player] = seconds .. " Seconds"
print(_G.timePlayed[player])
wait(1)
end
This wait is giving your processor time to rest. Your game runs fine. Unlike:
if minutes >= 1 and seconds <= 0 then
seconds = seconds + 1
_G.timePlayed[player] = minutes .. " Minutes and " .. seconds .. " Seconds"
elseif...
No wait in sight. Your code will loop and run the same code with 0 breaks. It's processing the math of adding 1 to the variable seconds, processing _G and changing the value the timeplayed table's key of player to the string and... You get the idea. A lot of work with no time for your computer to rest. Add a wait!
if minutes >= 1 and seconds <= 0 then
seconds = seconds + 1
_G.timePlayed[player] = minutes .. " Minutes and " .. seconds .. " Seconds"
wait(1)
elseif...
#_G
_G is the global table, but it's not used much anymore because it's over shadowed by ModuleScripts. Instead of using _G, use a module script instead.
local module = require(script.ModuleScript)
local minutes = 0
local seconds = 0
game.Players.PlayerAdded:Connect(function(player)
while wait(1) do
module:UpdateTime(player)
end
end)
Module Script:
local module = {}
module.timePlayed = {}
function module:UpdateTime(player)
module.timePlayed[player] = module.timePlayed[player]+1
end
return module
Other script:
local module = require(game:GetService("ServerScriptService").Script.ModuleScript)
function decodeTime(player)
local seconds = module.timePlayed[player]
if seconds < 60 then
print(tostring(seconds).." Seconds")
else
print(tostring(math.floor(seconds/60)).." Minutes and "..tostring(seconds%60).." Seconds")
end
end
tostring turns everything into a string. Booleans, numbers, nil and etc. math.floor rounds the number down to the nearest whole number and % is modulus. Basically, it gets the remainder of a number/number2.
Hope it helps!