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

How to use TimeOfDay?

So, for my game I am teleporting all of the players into a certain area of my map.

In a separate script I have the time changing.

Here is my code:

local target = CFrame.new(207.73, 1.5, 4065.287)
local tod = game.Lighting.TimeOfDay
game.Lighting.Changed:connect(function(TimeOfDay)
	if tod == "00:03:20" then
		print("Time if passed.")
		for i, player in ipairs(game.Players:GetChildren()) do
			if player.Character and player.Character:FindFirstChild("Torso") then
				print("char check and torso check passed.")
		    	player.Character.Torso.CFrame = target + Vector3.new(0, i * 5, 0)
		   end
		end
	end
end)

So, am I just using time of day wrong? I'm thinking the if statement isn't working but I'm not sure how to fix it.

Oh and, there are also no errors

2 answers

✓ Accepted answer

Your Problem

When you define tod on line 2 then the value becomes static, meaning it will never change unless manually changed. If you want the value to change then don't index TimeOfDay when defining 'tod', but index it when checking 'tod'.


Code

local target = CFrame.new(207.73, 1.5, 4065.287)
local tod = game.Lighting

game.Lighting.Changed:connect(function(TimeOfDay)
	if tod.TimeOfDay == "00:03:20" then
		print("Time if passed.")
		for i, player in ipairs(game.Players:GetChildren()) do
			if player.Character and player.Character:FindFirstChild("Torso") then
				print("char check and torso check passed.")
				player.Character.Torso.CFrame = target + Vector3.new(0, i * 5, 0)
			end
		end
	end
end)

1 comment

0

Thanks dude! With a few changes, this has ended up working perfectly.

You should not set a variable to a value of an object. You should use the .Changed function on the object and then check if its value is different.

local target = CFrame.new(207.73, 1.5, 4065.287)
local lighting = game.Lighting

lighting.Changed:connect(function()
    if lighting.TimeOfDay == "00:03:20" then
        for i,v in next,game.Players:GetChildren() do
        	v.Character.Torso.CFrame = target + Vector3.new(0, i * 5, 0)
        end
    end
end)

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