Trying to get sky in my game to turn from nighttime to daytime while in a specific area

So I’m about to add this map to my game that’s primarily in nighttime mode for the most part, but this new map looks a lot better during the daytime so I wanna try and make it so that if you spawn in that map it will be daytime rather than nighttime.

Problem being though, I can’t seem to get it to work, no errors really pop up in the console so I can’t even really gauge what’s going wrong with the code.

Here is the code that’s supposed to handle the ClockTime changes:

wait(0.1)

local Player = game.Players.LocalPlayer
repeat wait() until Player.Character
local Character = Player.Character --Waits for the Player to load in

local val = Character.area --Grabs the "area" value, an IntValue that changes whenever you click on a specific map to select in the character selection screen

local function onValueChanged(newValue)
	if val.Value == 3 then
		game.Lighting.ClockTime = 14 --If the value is set to this new map then it should make it daytime in the game for only players that entered that map
	end
end

val.Changed:Connect(onValueChanged)

I didn’t know if it was like an issue with where I had the script exactly so first I had it inside of StarterCharacterScripts, but then I realized the script that I had borrowed some of the code from was originally in StarterGui so I moved it there but it only provided the same results.
I think it’s supposed to be LocalScript? Because when I changed it to be a normal script running in Client context it popped up an orange message in the console saying “Client based script in StarterGui may cause it to run multiple times, consider changing to Legacy” something along those lines.

If anybody has any advice for solving this issue then that would be greatly appreciated, thank you ! ^ ^

Hello !

So I think the problem is because of this line :

val.Changed:Connect(onValueChanged)

The function will never execute when the player spawn, because it waits for a value change. To solve this problem, try this:

wait(0.1)

local Player = game.Players.LocalPlayer
repeat wait() until Player.Character
local Character = Player.Character --Waits for the Player to load in

local val = Character.area --Grabs the "area" value, an IntValue that changes whenever you click on a specific map to select in the character selection screen

local function onValueChanged(newValue)
	if val.Value == 3 then
		game.Lighting.ClockTime = 14 --If the value is set to this new map then it should make it daytime in the game for only players that entered that map
	end
end

val.Changed:Connect(onValueChanged)

-- Check the current value immediately
onValueChanged()

To finish, if you want only the player who entered map 3 to see daytime, then yes, a LocalScript is appropriate.

I’d put it in StarterPlayer → StarterPlayerScripts.

Have a nice day !

1 Like

Hm, I changed the code in the script to that and moved it to StarterPlayerScripts but it still would not change the ClockTime.

I moved it back into StarterGui and it works perfectly now ! Tysm for helping me out with this ! ^ ^

1 Like

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