Values doesn't decreasing when it's meet all requirements

Hi, I’m making play time in my game, but something isn’t working. It’s just counting the seconds, but if you’ve reached 60 seconds it’s not decreasing and adding +1 to minutes. Script:

game.Players.PlayerAdded:Connect(function(Player)
	while wait(1) do
		Player:WaitForChild("Time"):WaitForChild("Seconds").Value += 1
	end
	
	Player:WaitForChild("Time"):WaitForChild("Seconds").Changed:Connect(function()
		if Player:WaitForChild("Time"):WaitForChild("Seconds").Value >= 60 then
			Player:WaitForChild("Time"):WaitForChild("Seconds").Value -= 60
			Player:WaitForChild("Time"):WaitForChild("Minutes").Value += 1
		end
	end)

	Player:WaitForChild("Time"):WaitForChild("Minutes").Changed:Connect(function()
		if Player:WaitForChild("Time"):WaitForChild("Minutes").Value >= 60 then
			Player:WaitForChild("Time"):WaitForChild("Minutes").Value -= 60
			Player:WaitForChild("Time"):WaitForChild("Hours").Value += 1
		end
	end)

	Player:WaitForChild("Time"):WaitForChild("Hours").Changed:Connect(function()
		if Player:WaitForChild("Time"):WaitForChild("Hours").Value >= 24 then
			Player:WaitForChild("Time"):WaitForChild("Hours").Value -= 24
			Player:WaitForChild("Time"):WaitForChild("Days").Value += 1
		end
	end)

	Player:WaitForChild("Time"):WaitForChild("Days").Changed:Connect(function()
		if Player:WaitForChild("Time"):WaitForChild("Days").Value >= 7 then
			Player:WaitForChild("Time"):WaitForChild("Days").Value -= 7
			Player:WaitForChild("Time"):WaitForChild("Weeks").Value += 1
		end
	end)

	Player:WaitForChild("Time"):WaitForChild("Weeks").Changed:Connect(function()
		if Player:WaitForChild("Time"):WaitForChild("Weeks").Value >= 4 then
			Player:WaitForChild("Time"):WaitForChild("Weeks").Value -= 4
			Player:WaitForChild("Time"):WaitForChild("Months").Value += 1
		end
	end)

	Player:WaitForChild("Time"):WaitForChild("Months").Changed:Connect(function()
		if Player:WaitForChild("Time"):WaitForChild("Months").Value >= 12 then
			Player:WaitForChild("Time"):WaitForChild("Months").Value -= 12
			Player:WaitForChild("Time"):WaitForChild("Years").Value += 1
		end
	end)
end)

Help pls!

2 Likes

I believe I see the problem, but can I see the script where the “Time” and “Seconds” and “Minute” values are initialized?

1 Like
game.Players.PlayerAdded:Connect(function(Player)
	local Time = Instance.new("Folder")
	Time.Name = "Time"
	Time.Parent = Player
	
	local Seconds = Instance.new("IntValue")
	Seconds.Name = "Seconds"
	Seconds.Parent = Time
	
	local Minutes = Instance.new("IntValue")
	Minutes.Name = "Minutes"
	Minutes.Parent = Time
	
	local Hours = Instance.new("IntValue")
	Hours.Name = "Hours"
	Hours.Parent = Time
	
	local Days = Instance.new("IntValue")
	Days.Name = "Days"
	Days.Parent = Time
	
	local Weeks = Instance.new("IntValue")
	Weeks.Name = "Weeks"
	Weeks.Parent = Time
	
	local Months = Instance.new("IntValue")
	Months.Name = "Months"
	Months.Parent = Time
	
	local Years = Instance.new("IntValue")
	Years.Name = "Years"
	Years.Parent = Time
end)

game.Players.PlayerAdded:Connect(function(Player)
	local Data = game:GetService("DataStoreService"):GetDataStore(Player.UserId.."Data"):GetAsync("Time")
	Player:WaitForChild("Time"):WaitForChild("Seconds").Value = Data.Seconds
	Player:WaitForChild("Time"):WaitForChild("Minutes").Value = Data.Minutes
	Player:WaitForChild("Time"):WaitForChild("Hours").Value = Data.Hours
	Player:WaitForChild("Time"):WaitForChild("Days").Value = Data.Days
	Player:WaitForChild("Time"):WaitForChild("Weeks").Value = Data.Weeks
	Player:WaitForChild("Time"):WaitForChild("Months").Value = Data.Months
	Player:WaitForChild("Time"):WaitForChild("Years").Value = Data.Years
end)

game.Players.PlayerRemoving:Connect(function(Player)
	local Data = {
		["Seconds"] = Player:WaitForChild("Time"):WaitForChild("Seconds").Value,
		["Minutes"] = Player:WaitForChild("Time"):WaitForChild("Minutes").Value,
		["Hours"] = Player:WaitForChild("Time"):WaitForChild("Hours").Value,
		["Days"] = Player:WaitForChild("Time"):WaitForChild("Days").Value,
		["Weeks"] = Player:WaitForChild("Time"):WaitForChild("Weeks").Value,
		["Months"] = Player:WaitForChild("Time"):WaitForChild("Months").Value,
		["Years"] = Player:WaitForChild("Time"):WaitForChild("Years").Value
	}
	
	game:GetService("DataStoreService"):GetDataStore(Player.UserId.."Data"):SetAsync("Time", Data)
end)

The upper script works normally, and not writing any errors in console.

Please just track the seconds and convert it into Y:M:W:D:H:M:S via string manipulation :pray:

I don’t used this before and Idk how can I use it lol

Just look at the general idea and figure out how to apply it to your game. Your code is very inefficient

That looks sooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo hard lol

You gotta learn if you wanna make progress
And it’s easier than it looks, I believe in you

1 Like

The loop

while wait(1) do
    Player:WaitForChild("Time"):WaitForChild("Seconds").Value += 1
end

will never end, so the code after it (where you handle minutes, hours, etc.) never runs!

A simple solution would be to move your loop to after the code for minutes, hours, etc.

So for example:

PlayerAdded:Connect(function()
    seconds.Changed:Connect(...)
    minutes.Changed:Connect(...)

    while wait(1) do
        seconds += 1
    end
end)

But I’ve tried to remove this lines from the script and create another one for them, but it’s just not worked. But I’ll try it rn.

Lol, it’s actually the problem!

1 Like

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