Distance/Lightning based part not working properly

I’m having problems with one of the aspects of my game.

Basically, this ball, over here, doesn’t work so well.

When you enter the area of said ‘‘ball’’, the sky should turn dark and lightning effects will sometimes appear. (As shown in the following video.)

Here’s the script that causes the sky to go dark.

local players = game:GetService('Players')
local maxDistance = 241
local L = true

while task.wait(0.1) do
	for i, player in players:GetPlayers() do
		if player.Character then
			if player:DistanceFromCharacter(script.Parent.Position) < maxDistance then
				if L == true then
					L = false
					game.Lighting.Atmosphere.Density = 0.3
					game.Lighting.Atmosphere.Color = Color3.new(0.396078, 0.568627, 0.74902)
					game.Lighting.Atmosphere.Decay = Color3.new(0, 0.4, 0.74902)
					game.Lighting.Atmosphere.Haze = 0.1
					
					script["Rain Sound Effect"].Volume = 0.1
					task.wait(0.15)
					game.Lighting.Atmosphere.Density = 0.5
					game.Lighting.Atmosphere.Color = Color3.new(0.262745, 0.376471, 0.498039)
					game.Lighting.Atmosphere.Decay = Color3.new(0, 0.266667, 0.498039)
					game.Lighting.Atmosphere.Haze = 1
					
					script["Rain Sound Effect"].Volume = 0.2
					task.wait(0.15)
					game.Lighting.Atmosphere.Density = 0.7
					game.Lighting.Atmosphere.Color = Color3.new(0.129412, 0.188235, 0.247059)
					game.Lighting.Atmosphere.Decay = Color3.new(0, 0.133333, 0.247059)
					game.Lighting.Atmosphere.Haze = 5
					
					script["Rain Sound Effect"].Volume = 0.3
					task.wait(0.15)
					game.Lighting.Atmosphere.Density = 0.8
					game.Lighting.Atmosphere.Color = Color3.new(0, 0, 0)
					game.Lighting.Atmosphere.Decay = Color3.new(0, 0, 0)
					game.Lighting.Atmosphere.Haze = 2.75
					
					script["Rain Sound Effect"].Volume = 0.4
					
					game.Lighting.Brightness = 0
					game.Lighting.EnvironmentDiffuseScale = 0
					game.Lighting.Ambient = Color3.new(0, 0, 0)
					game.Lighting.OutdoorAmbient = Color3.new(0, 0, 0)
				end
			else
				if L == false then
					L = true
					game.Lighting.Atmosphere.Density = 0.7
					game.Lighting.Atmosphere.Color = Color3.new(0.129412, 0.188235, 0.247059)
					game.Lighting.Atmosphere.Decay = Color3.new(0, 0.133333, 0.247059)
					game.Lighting.Atmosphere.Haze = 5
					
					script["Rain Sound Effect"].Volume = 0.3
					task.wait(0.15)
					game.Lighting.Atmosphere.Density = 0.5
					game.Lighting.Atmosphere.Color = Color3.new(0.262745, 0.376471, 0.498039)
					game.Lighting.Atmosphere.Decay = Color3.new(0, 0.266667, 0.498039)
					game.Lighting.Atmosphere.Haze = 1
					
					script["Rain Sound Effect"].Volume = 0.2
					task.wait(0.15)
					game.Lighting.Atmosphere.Density = 0.3
					game.Lighting.Atmosphere.Color = Color3.new(0.396078, 0.568627, 0.74902)
					game.Lighting.Atmosphere.Decay = Color3.new(0, 0.4, 0.74902)
					game.Lighting.Atmosphere.Haze = 0.1
					
					script["Rain Sound Effect"].Volume = 0.1
					task.wait(0.15)
					game.Lighting.Atmosphere.Density = 0.25
					game.Lighting.Atmosphere.Color = Color3.new(0.529412, 0.756863, 1)
					game.Lighting.Atmosphere.Decay = Color3.new(0, 0.533333, 1)
					game.Lighting.Atmosphere.Haze = 0.01
					
					script["Rain Sound Effect"].Volume = 0
					
					game.Lighting.Brightness = 4
					game.Lighting.EnvironmentDiffuseScale = 1
					game.Lighting.Ambient = Color3.new(0.243137, 0.243137, 0.243137)
					game.Lighting.OutdoorAmbient = Color3.new(0.27451, 0.27451, 0.27451)
				end
			end
		end
	end
end

Here’s the script that causes the lightning sounds.

local players = game:GetService('Players')
local maxDistance = 241
local L = true

while task.wait(0.1) do
	for i, player in players:GetPlayers() do
		if player.Character then
			if player:DistanceFromCharacter(script.Parent.Position) < maxDistance then
				if L == true then
					L = true
					if math.random(1,3) == 1 then
						local AssetFolder = script

						local Sfx1 =  AssetFolder:GetChildren()[math.random(1, #AssetFolder:GetChildren())]
						
						local Sfx = Sfx1:Clone()
						
						game.Lighting.Brightness = math.random(3,10)
						Sfx.Parent = script
						
						Sfx.Playing = true
						
						Sfx.PlaybackSpeed = math.random(90,110)/100
						Sfx.Volume = math.random(40,60)/100
						
						task.wait(0.2)
						game.Lighting.Brightness = 0
						task.wait(math.random(1,10))
						Sfx:Destroy()
					end
				end
			end
		end
	end
end

The issue here, I struggle a little explaining it, but it works outside said area, without even going inside first. (Also as show in the following video.)

:warning: EPILEPSY WARNING :warning: (Applies at 0:08, also where the bug starts.)

I don’t know how I could physically fix the script, but it seems to only happen when I’m too far from the part that it unloads from the workspace. I have searched a little bit the forum for answers, but nothing helped. (Edit : It works perfectly fine in roblox studio.)

If anyone has a solution, or a post to redirect me to, I’d be glad!

Thank you!

I am still unable to find a solution but I’ll survive I hope :pensive_face:

Still have no idea how to fix this bug, please help :folded_hands::sob:

You can make it so the part doesn’t unload from the workspace by putting it in a Model and setting the model to Model.ModelStreamingMode = Enum.ModelStreamingMode.Persistent.

Also, your scripts trigger the sounds and brightness effects if any character is within the distance range. You probably want to go into the Scripts and set their run context to client and then only look at the Players.LocalPlayer. (Use a Script so they can still run in Workspace, unlike a LocalScript, but then set the run context so they run on the client/locally.) (This makes it so the effects only show on a single player’s computer at at time, based on where their character is.)

If neither of these fix your problem, you could put some debugging print statements in your code and use the developer console to see what’s going on in game.

1 Like

THANK YOU SO MUCH!

The Model.ModelStreamingMode thingy worked perfectly.

My scripts were already in a client sided script, so that’s good.

Though, there was a little issue with the part’s transparency once in game, but I was gladfully able to fix this quite easily.

Thank you again!

1 Like

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