Problems with my Local Environment Script

Hi devforum, I’m having problems with my Local Environment Script. When your ship enters a planet (a Region3), the lighting changes then goes back to the default almost immediately as shown in the video below.

I don’t understand why I have this issue, I have tried researching all over the web but could not find any solution to what I have. Any help is appreciated, thank you.

Code:

while true do wait(.25)
	for i,v in pairs(game.Workspace:GetDescendants()) do
		if v.Name == "LocalEnvironment" then
			local regionPart = v
			local region = Region3.new(regionPart.Position-(regionPart.Size/2), regionPart.Position+(regionPart.Size/2))

			local obj = workspace:FindPartsInRegion3(region,nil,math.huge)
			for i,parts in pairs(obj) do
				if parts.Parent.Parent.Parent == game.Workspace.GaloidWorkspace.PlayerShips:FindFirstChild(player.Name) then
					if inenvironment == false then
						inenvironment = true
						enter(v)
					end
				else
					if inenvironment == true then
						inenvironment = false
						exit(v)
					end
				end
			end
		end
	end
end

you should adjust your code to do

infinite loop
   isInsideARegion = false
   for each region
      if the player is inside
         use the enter lighting
         isInsideARegion = true
      end
   end
   if not isInsideARegion then
      use the exit lighting
   end
end

your code currently detects that you’re inside a region,
but then after continuing the loop to another region that you’re not inside of, it says that you’re outside the region

this is how your code is working

infinite loop
   isInsideARegion = false
   for each region
      if the player is inside
         use the enter lighting
         isInsideARegion = true
      else
         use the exit lighting
         isInsideARegion = false
      end
   end
end
1 Like