Night & Day Script + Time Save Script = Break

  1. What do you want to achieve? Keep it simple and clear!
    -I am aiming to make a game that can both save the amount of time in minutes that the player is in-game, and use a day/night script.

  2. What is the issue? Include screenshots / videos if possible!
    -My script for saving time did work initially, but then it broke after I added a day/night script.

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    -I’m guessing the issue is that the day/night script speeds up the world time while the time saving script is supposed to keep track of that time? That’s my guess but I’m not able to figure out how to use both without one breaking.
    -I have yet to see a game that uses both of these at the same time, and no I haven’t seen a question like this in the forum either.

Any insight from pros is appreciated!

Can you please send the scripts so that I can see what is going on? Otherwise I can’t help you

Sure, this is the Time saving script

local datastore = game:GetService("DataStoreService"):GetService("")

local joinTimes = {} 

game:GetService("Players").PlayerAdded:Connect(function(player)

	local leaderstats = Instance.new("Folder", player)
	
	leaderstats.Name = "leaderstats"

	local playTime = Instance.new("IntValue", leaderstats)
	playTime.Name = "Time Played"

	local key = player.UserId
	local success, data = pcall(datastore.GetAsync, datastore, key)
	

	if success then
		playTime.Value = data or 0
		joinTimes[player] = {data or 0, tick()}
		
		warn(data)
		
	end
end)

game:GetService("Players").PlayerRemoving:Connect(function(player)
	
	local key = player.UserId
	local success, error = pcall(datastore.IncrementAsync, datastore, math.floor((tick() - joinTimes[player][2])/ 60))
	

	if not success then
		warn("UpdateAsync failed for", player, ":", error)
		
	end
end)

game:GetService("RunService").Heartbeat:Connect(function()
	
	local now = tick()
	for player, times in pairs(joinTimes) do
		local leaderstats = player:FindFirstChild("leaderstats")
		if leaderstats and leaderstats:FindFirstChild("Time Played") then
			leaderstats["Time Played"].Value = times[1] + math.floor((now - times[2]) / 60)
			
		end
	end
end)

And this would be the Day/Night Script.

local lighting = game:GetService("Lighting")
local ts = game:GetService("TweenService")

local transition = 120
local ti = TweenInfo.new(transition, Enum.EasingStyle.Quad, Enum.EasingDirection.InOut)
local day = 2
local night = 2

while true do
	--Tween to Night--
	local t1 = ts:Create(lighting, ti, {ClockTime =0})
	t1:Play()
	t1.Completed:Wait()
	
	--Wait--
	wait(night)
	
	--Tween to Day--
	local t2 =  ts:Create(lighting, ti, {ClockTime =14})
	t2:Play()
	t2.Completed:Wait()

	--Wait--
	wait(day)
end
1 Like

I forgot to add, can I see the output if there are any errors?

I’m not sure if you’re able to zoom in on that or not, but there are only those two errors when I run the scripts.

Based on the datastore warning that was provided, it seems like the datastore API isn’t enabled for your game, which is easily togglable in the game settings.

This is what I found for your error (403)


Source: Data Stores

Here’s how to enable the service in the game settings:


Source: Data Stores

If the error persists, try it in the real game rather than studio, since data typically doesn’t save in studio correctly unless you use a game:BindToClose() function, add a wait and save data from there. If the error persists even further, you can respond to this message and I can take a look at your code, but for now make sure these steps are done before anything :slight_smile:

2 Likes