Changing lighting on the client side when stepping into different zones

Hello,

I’m trying to create a script which changes the ambient lighting based on which zone you are in (part located in the workspace). I’ve gotten everything to work properly but the lighting changes on the server side for all players.

I’ve been trying to use remote events but I’m a little confused on how to keep it on the client side.

Script in StarterCharacterScripts


Lighting = game:GetService('Lighting')
local TweenService = game:GetService("TweenService")
local player = game.Players.LocalPlayer
local City = false
local County = false

game.Workspace.GameNetwork.Zones.City.Touched:Connect(function(partTouched)
	if partTouched and partTouched.Parent and partTouched.Parent:FindFirstChildOfClass("Humanoid") then
		City = true
		local player = game.Players:FindFirstChild(partTouched.Parent.Name)
		local playerGui = player:FindFirstChild("PlayerGui")
		local Frame = playerGui:WaitForChild("Main"):WaitForChild("LocationFrame")
		local Accent = Frame:WaitForChild("Accent")
		local Welcome = Frame:WaitForChild("WelcomeLabel")
		local Location = Frame:WaitForChild("LocationName")
		local TweenInfo = TweenInfo.new(1, Enum.EasingStyle.Back, Enum.EasingDirection.In)
		Location.Text = "City Of Cambridge"
		local LocationTween = TweenService:Create(Location, TweenInfo, {TextTransparency = 0})
		local WelcomeTween = TweenService:Create(Welcome, TweenInfo, {ImageTransparency = 0})
		local AccentTween = TweenService:Create(Accent, TweenInfo, {BackgroundTransparency = 0})
		LocationTween:Play()
		WelcomeTween:Play()
		AccentTween:Play()
		task.wait(2)
		local LocationTweenOut = TweenService:Create(Location, TweenInfo, {TextTransparency = 1})
		local WelcomeTweenOut = TweenService:Create(Welcome, TweenInfo, {ImageTransparency = 1})
		local AccentTweenOut = TweenService:Create(Accent, TweenInfo, {BackgroundTransparency = 1})
		LocationTweenOut:Play()
		WelcomeTweenOut:Play()
		AccentTweenOut:Play()
	end
end)

game.Workspace.GameNetwork.Zones.City.TouchEnded:Connect(function(partTouched)
	if partTouched and partTouched.Parent and partTouched.Parent:FindFirstChildOfClass("Humanoid") then
		City = false
	end
end)

game.Workspace.GameNetwork.Zones.County.Touched:Connect(function(partTouched)
	if partTouched and partTouched.Parent and partTouched.Parent:FindFirstChildOfClass("Humanoid") then
		County = true
		local player = game.Players:FindFirstChild(partTouched.Parent.Name)
		local playerGui = player:FindFirstChild("PlayerGui")
		local Frame = playerGui:WaitForChild("Main"):WaitForChild("LocationFrame")
		local Accent = Frame:WaitForChild("Accent")
		local Welcome = Frame:WaitForChild("WelcomeLabel")
		local Location = Frame:WaitForChild("LocationName")
		local TweenInfo = TweenInfo.new(1, Enum.EasingStyle.Back, Enum.EasingDirection.In)
		Location.Text = "Wasaga Beach"
		local LocationTween = TweenService:Create(Location, TweenInfo, {TextTransparency = 0})
		local WelcomeTween = TweenService:Create(Welcome, TweenInfo, {ImageTransparency = 0})
		local AccentTween = TweenService:Create(Accent, TweenInfo, {BackgroundTransparency = 0})
		LocationTween:Play()
		WelcomeTween:Play()
		AccentTween:Play()
		task.wait(2)
		local LocationTweenOut = TweenService:Create(Location, TweenInfo, {TextTransparency = 1})
		local WelcomeTweenOut = TweenService:Create(Welcome, TweenInfo, {ImageTransparency = 1})
		local AccentTweenOut = TweenService:Create(Accent, TweenInfo, {BackgroundTransparency = 1})
		LocationTweenOut:Play()
		WelcomeTweenOut:Play()
		AccentTweenOut:Play()
	end
end)

game.Workspace.GameNetwork.Zones.County.TouchEnded:Connect(function(partTouched)
	if partTouched and partTouched.Parent and partTouched.Parent:FindFirstChildOfClass("Humanoid") then
		County = false
	end
end)

while true do
	wait()
	if City == true then
		local info = TweenInfo.new(1, Enum.EasingStyle.Linear, Enum.EasingDirection.InOut, 0, false, 0)
		local Brightinfo = TweenInfo.new(1.8, Enum.EasingStyle.Linear, Enum.EasingDirection.InOut, 0, false, 0)
		local Night = false
		local Day = false
		if Lighting.ClockTime >= 17 or Lighting.ClockTime < 6 then
			Night = true
		else
			Day = true
		end
			if Night == true then
				local BrightTween = TweenService:Create(game.Lighting, Brightinfo, {Brightness = 1})
				BrightTween:Play()
				BrightTween.Completed:Wait()

				local BrightTween = TweenService:Create(game.Lighting, info, {Ambient = Color3.fromRGB(52,52,52)})
				BrightTween:Play()
				BrightTween.Completed:Wait()
			elseif Day == true then
				local BrightTween = TweenService:Create(game.Lighting, Brightinfo, {Brightness = 2})
				BrightTween:Play()
				BrightTween.Completed:Wait()

				local BrightTween = TweenService:Create(game.Lighting, info, {Ambient = Color3.fromRGB(71, 71, 71)})
				BrightTween:Play()
				BrightTween.Completed:Wait()
			end
		end
	if County == true then
		local info = TweenInfo.new(1, Enum.EasingStyle.Linear, Enum.EasingDirection.InOut, 0, false, 0)
		local Brightinfo = TweenInfo.new(1.8, Enum.EasingStyle.Linear, Enum.EasingDirection.InOut, 0, false, 0)
		local Night = false
		local Day = false
		if Lighting.ClockTime >= 17 or Lighting.ClockTime < 6 then
			Night = true
		else
			Day = true
		end

		if Night == true then
				local BrightTween = TweenService:Create(game.Lighting, Brightinfo, {Brightness = 1})
				BrightTween:Play()
				BrightTween.Completed:Wait()

				local BrightTween = TweenService:Create(game.Lighting, info, {Ambient = Color3.fromRGB(32,32,32)})
				BrightTween:Play()
				BrightTween.Completed:Wait()
		elseif Day == true then
				local BrightTween = TweenService:Create(game.Lighting, Brightinfo, {Brightness = 2})
				BrightTween:Play()
				BrightTween.Completed:Wait()

				local BrightTween = TweenService:Create(game.Lighting, info, {Ambient = Color3.fromRGB(78, 78, 78)})
				BrightTween:Play()
				BrightTween.Completed:Wait()
			end
		end
	end

Any help would be greatly appreciated.

i have a script that does this, you dont need remote events at all if you wanted to keep it client. Just simply look for touch and change the lighting properties accordingly, but if you wanted it to not be so sudden, you can do a 1 second tween to change the properties over.

local LightParts = workspace:WaitForChild("LightParts")
local Lighting = game:GetService("Lighting")
local Players = game:GetService("Players")
local LocalPlayer = Players.LocalPlayer
local Character = LocalPlayer.Character or LocalPlayer.CharacterAdded:Wait()
local HumanoidRootPart = Character:WaitForChild("HumanoidRootPart")

local LightingPresets = {
	Spawn = {
		Brightness = 2.5, 
		ClockTime = 14, 
		FogEnd = 800, 
		Ambient = Color3.fromRGB(200, 200, 220), 
		OutdoorAmbient = Color3.fromRGB(180, 180, 201), 
		ExposureCompensation = 0.2, 
		ColorShift_Top = Color3.fromRGB(220, 200, 180),
		ColorShift_Bottom = Color3.fromRGB(240, 230, 210) 
	},
	Default = {
		Brightness = 3,
		ClockTime = 14.5,
		FogEnd = 5000,
		Ambient = Color3.fromRGB(67, 67, 67),
		ColorShift_Top = Color3.fromRGB(0,0,0),
		ColorShift_Bottom = Color3.fromRGB(0,0,0), 
		OutdoorAmbient = Color3.fromRGB(70,70,70),
		ExposureCompensation = 0, 
	}
}

local function ApplyLighting(settings)
	for property, value in pairs(settings) do
		Lighting[property] = value
	end
end

local activeZone = nil 

for _, part in ipairs(LightParts:GetChildren()) do
	if part:IsA("BasePart") then
		part.Touched:Connect(function(hit)
			local character = LocalPlayer.Character
			if character and hit:IsDescendantOf(character) then
				activeZone = part.Name
				local settings = LightingPresets[part.Name] or LightingPresets.Default
				ApplyLighting(settings)
			end
		end)
		part.TouchEnded:Connect(function(hit)
			local character = LocalPlayer.Character
			if character and hit:IsDescendantOf(character) then
				if not activeZone then
					ApplyLighting(LightingPresets.Default)
				end
				activeZone = nil
			end
		end)
	end
end

this script is in starterplayers, feel free to use i!

1 Like

Hi, thanks for your reply. Do you have any suggestions on how to make the time of day checking clean? Right now it’s just using a while true do loop.

I have a lighting module that i will send you when I’m home. Iirc it’s 0.02 light time change per 3 seconds or soemthing like that

Thanks. I currently have my own time-changing script, and I am just wondering how to incorporate my old script into yours.

These are the changes I made to your script:

while true do
wait()
	if Lighting.ClockTime >= 17 or Lighting.ClockTime < 6 then
		Night = true
	else
		Day = true
	end
end

local LightingPresets = {
	Default = {
		BrightnessNight = Color3.fromRGB(1), 
		AmbientNight = Color3.fromRGB(32, 32, 32),
		BrightnessDay = Color3.fromRGB(2), 
		AmbientDay = Color3.fromRGB(71, 71, 71)
	},
	City = {
		BrightnessNight = Color3.fromRGB(1), 
		AmbientNight = Color3.fromRGB(52, 52, 52),
		BrightnessDay = Color3.fromRGB(2), 
		AmbientDay = Color3.fromRGB(71, 71, 71)
	}
}

I just don’t know how I’d make it use either Day or Night Ambient and Brightness depending on the time of day. Specifically Lighting[property] = value - I don’t really understand how this works and how I could add a if statement to use the other values.

 
local TimeCycler = {}

local Data = require(game.ReplicatedStorage.Modules.Shared.Data)
local RunService = game:GetService("RunService")
local LightingService = game:GetService("Lighting")

local LastCheck = tick()

function TimeCycler:IsNight()
	return LightingService.ClockTime >= Data.NightTime[1] or LightingService.ClockTime <= Data.NightTime[2]
end

function TimeCycler:UpdateTime()
	local timeSpeed = TimeCycler:IsNight() and Data.NightSpeed or Data.DaySpeed
	LightingService.ClockTime += timeSpeed
end

function TimeCycler:CheckTimeInterval()
	local CurrentCheck = tick()
	if CurrentCheck - LastCheck >= Data.LightUpdateInvertval then
		LastCheck = CurrentCheck
		self:UpdateTime()
	end
end

function TimeCycler:Start()
	RunService.Stepped:Connect(function()
		TimeCycler:CheckTimeInterval()
	end)
end

TimeCycler:Start()

return TimeCycler

This is my time cycler. I thought you were trying to change the lighting based on part touching beforehand? If its based on clocktime it would be the server

I’m changing the Ambient and Brightness based on what part you’re touching, but this also changes based on if it is night or day.

What I mean is that before you invoke the lighting change on the player it asks if its night or day and applies the correct lighting

Example:

if Night == true then
Lighting[property] = (NightValue)
else