Is there a way to optimize this pretty bad code?

Im new to lua. Basically I made this proximity prompt script so whenever it is triggered it activates night mode. The problem is that it works only when you trigger it like for the third time .
here is the script:

local prompt = script.Parent.ProximityPrompt
prompt.Triggered:Connect(function(player)
	local lighting = script.Parent.Parent.Parent.Lighting
	prompt.Triggered:Connect(function()
		if prompt.ActionText == "Night Mode" then
			lighting.ClockTime = 24
			prompt.ActionText = "Day"
			script.Parent.Parent.Parent.Lighting.FogColor = Color3.fromRGB(0, 0, 0)
			for _, ff in workspace.TerrainPlate:GetChildren() do
				ff.Enabled = true
			end
		else
			lighting.ClockTime = 10
			prompt.ActionText = "Night Mode"
			for _, ff in workspace.TerrainPlate:GetChildren() do
				ff.Enabled = false
			end
		end
	end)
end)

Why did you repeat prompt.Triggered?

What is the TerrainPlate and what are its children?
And what is the day time fog colour?

This is the basic script. And why did you repeat Triggered event. I removed other part of script, you can add them later. Here is the script:

local lighting = game:GetService("Lighting")
local prompt = script.Parent.ProximityPrompt

prompt.Triggered:Connect(function(player)
	
	if prompt.ActionText == "Night Mode" then
		lighting.ClockTime = 24
		prompt.ActionText = "Day"
--I removed them you can add later on
	elseif prompt.ActionText == "Day" then
		lighting.ClockTime = 10
		prompt.ActionText = "Night Mode"
--I removed them you can add later on
	end
	
end)

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