Script not detecting changes in fog

I am building a bus and have scripted working foglamps. To give the effect of them cutting through fog I used Beams to do it. The only thing is I do not want the beams activating when it is not foggy.

The beams on the foglamps will stay deactivated/activated depending on what game.Lighting.Fogend was at the START of the server. (The script that checks fogend is not responsing to changes.)
Here is a video showing the issue: https://youtu.be/k3dmUvPCsH4

I tried using a different script on a while true do loop to check the FogEnd value every 5 seconds but after changing the value in lighting the checking script did not update the value to what I set it to. I am out of ideas so is why I am writing this topic.
Here is the script for the foglamps.

The script

The value “lit” is triggered when the player presses the key that controls the foglamps
(Another script sets the value to toggle the lights on/off)

local Lighting = game:GetService("Lighting")
H = script.Parent

function onChanged()
	if H.Lit.Value == true then
		local children = script.Parent:GetChildren()
		for c = 1, #children do
		if children[c].Name == "H" then
			children[c].PointLight.Enabled = true	
			children[c].Transparency = 0
			if Lighting.FogEnd <= 1000 then
				children[c].light.Enabled = true
			end
		end
	end
	elseif H.Lit.Value == false then
		local children = script.Parent:GetChildren()
		for c = 1, #children do
		if children[c].Name == "H" then
				children[c].PointLight.Enabled = false
		children[c].light.Enabled = false
			children[c].Transparency = 0.7
		end
		end
	end
end

script.Parent.Lit.Changed:connect(onChanged)

I am not sure if I am understanding you correctly, but I will give it a shot.

Assuming this is referring to the script you provided us, keep in mind that you connected the Changed event to the boolean value “Lit”. The function onChanged() will only fire every time the Lit value is updated. It will not fire just because the FogEnd changes. You could connect a separate function to the GetPropertyChangedSignal of FogEnd to check when FogEnd specifically changes.

I assume this will not solve your problem, but I can help further if you can give me some more information.

As @Cu_beast mentioned, you can use the GetPropertyChangedSignal method to get a signal which will fire only when the given property is changed. Another way would be to utilize the Changed event’s first parameter, which is the name of the property that has changed. Here’s my take at your problem.

function onChanged(property)
    if property == "FogEnd" or property == "FogStart" then
       -- the fog has changed and you can do something here 
   end
end

The idea is that the lights will switch on. Just not the beam effects. The if statement inside of the function is supposed to check the FogEnd then switch the beam on along with the lights. It is just that If I change the value of FogEnd inside of studio while the server is live (testing) the if statement still does not think the FogEnd has changed. I also tried to make a script that just changes FogEnd every 10 seconds on a while true do loop and the Fog did nothing.

edit: Here is another video showing how the lights react when it is foggy. Note that the beams still activate with the lights after I change the fog value. In other words the script still thinks it is foggy.

Is the code in a server-sided script?

Yes. It is just a plain script inside of the model which contains all the foglights. I am confident that it is not a FE issue.

In your video, you’re changing the FogEnd value on the client, which the server naturally won’t detect. To change the value on the server, you will have to hit image during playtest to switch between seeing the game from the server’s and client’s perspective.

Thanks. I’m going to test now using this new information. Will post the results once I have done testing.

Edit: I think I can say that it works now.

Also, the way you coded it makes it check what the .FogEnd value is, only once when the headlights are turned on. Meaning, if the .FogEnd gets changed while the headlights are on, they won’t adjust to it before you turn them off and then on again.

Another thing I noticed is that you’re avoiding i, v in pairs loops - which are basically meant to be used to iterate through arrays with ease. An example for this which you could’ve used in your code is something like this…

for i, c in pairs(script.Parent:GetChildren()) do
	if c.Name == "H" then
		c.PointLight.Enabled = true	
		c.Transparency = 0
		if Lighting.FogEnd <= 1000 then
			c.light.Enabled = true
		end
	end
end
1 Like