- What do you want to achieve?
I’m trying to make a timer stored in a DataStoreService and the timer is stated in the TextLabel
-
What is the issue?
TextLabel won’t change with no errors and when the seconds become 0, an error occurs.
I couldn’t find any problems or solutions to this, so please help me!
Code
local StarterGui = game:GetService("StarterGui")
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
local DataStoreService = game:GetService("DataStoreService")
local CountdownTesting = DataStoreService:GetDataStore("CountdownTestingg")
local Gui = StarterGui.CountdownGui
local TextLabel = Gui.TextLabel
local totalweight = 0
local itemTable = {
percentageRarity = {70, 20, 9, 1},
items = {
{
name = math.random(1,10);
rarity = "Common";
},
{
name = math.random(50,99);
rarity = "Uncommon";
},
{
name = math.random(100, 500);
rarity = "Rare";
},
{
name = math.random(1000000, 5000000);
rarity = "Legendary";
}
}
}
local function SecondsToString(seconds)
local days = math.floor(seconds / 86400)
local hours = math.floor(seconds % 86400 / 3600)
local minutes = math.floor(seconds % 3600 / 60)
seconds = math.floor(seconds % 60)
if days >= 1 and hours >= 1 and minutes >= 1 and seconds >= 1 then
return string.format("%d days, %02d hours, %02d minutes, %02d seconds", days, hours, minutes, seconds)
elseif days <= 0 and hours >= 1 and minutes >= 1 and seconds >= 1 then
return string.format("%d hours, %02d minutes, %02d seconds", hours, minutes, seconds)
elseif days <= 0 and hours <= 0 and minutes >= 1 and seconds >= 1 then
return string.format("%d minutes, %02d seconds", minutes, seconds)
elseif days <= 0 and hours <= 0 and minutes <= 1 and seconds >= 1 then
return string.format("%d seconds", seconds)
end
end
local function onPlayerAdded(player)
for _, v in ipairs(itemTable.percentageRarity) do
totalweight = totalweight + v
end
local at = math.random() * totalweight
for key, v in pairs(itemTable.percentageRarity) do
if at < v then
local countdowntimer = itemTable.items[key].name
print(countdowntimer)
local success, value = pcall(CountdownTesting.SetAsync, CountdownTesting, "TimeLeft", os.time() + countdowntimer)
local successs, valuee = pcall(CountdownTesting.GetAsync, CountdownTesting, "TimeLeft")
if successs == false then return end
while true do
local deltaTime = valuee - os.time()
if deltaTime > 0 then
print(SecondsToString(deltaTime))
TextLabel.Text = SecondsToString(deltaTime)
end
if deltaTime <= 0 then
print("works")
end
task.wait(1)
end
break
end
at = at - v
end
end
Players.PlayerAdded:Connect(onPlayerAdded)