Weird highlight behavior

  1. What do you want to achieve? I’m trying to make some “elevators” that players can enter through

  2. What is the issue? Random highlight changing

  3. What solutions have you tried so far? I’ve searched everywhere but I couldn’t find a single thing

So basically I’m trying to make a game that uses “elevators” to teleport people into places where you can play your game (Kinda like a story game). The problem is that when one elevator gets full, it needs to only highlight the elevator that gets full. Instead it highlights itself with another random elevator. I’ve debugged my code and I saw that it only tries to highlight one elevator, where out of the blue the other one gets highlighted too. When I try to change the color normally using the console nothing happens.

Here is my code:

local TeleportService = game:GetService("TeleportService")

local Players = game:GetService("Players")

local GameInstances = {}

local CreateGameInstance = function(GameInstance)
	local Region = GameInstance.Region

	local Highlight = GameInstance.Highlight.Highlight
	local SurfaceUI = GameInstance.Highlight.SurfaceUI

	local Count = SurfaceUI.Count

	local MaxCount = GameInstance.Values.MaxPlayerAmount.Value
	local Queue = {}

	local CountdownNumber = GameInstance.Values.Countdown.Value

	local CountdownStartTick;

	local GetCountdown = function()
		return CountdownNumber - (math.floor(tick() - CountdownStartTick))
	end
	
	table.insert(GameInstances, {["Run"] = function(self)
		self.Debounce = true
		
		local Objects = workspace:GetPartBoundsInBox(Region.CFrame, Region.Size)
		
		for _, Object in pairs(Objects) do
			if Players:GetPlayerFromCharacter(Object.Parent) and Object == Object.Parent.PrimaryPart then
				local Player = Players:GetPlayerFromCharacter(Object.Parent)
				
				if not table.find(Queue, Player) and #Queue < MaxCount then
					table.insert(Queue, Player)
				end
			end
		end
		
		for Index = #Queue, 1, -1 do
			local Player = Queue[Index]
			
			if not Player.Character then
				table.remove(Queue, Index)

				continue
			end

			if not table.find(Objects, Player.Character.PrimaryPart) then
				table.remove(Queue, Index)
			end
		end
		
		if #Queue > 0 and not CountdownStartTick then
			CountdownStartTick = tick()
		elseif #Queue == 0 then
			CountdownStartTick = nil
			
			SurfaceUI.Count.Text = #Queue.." / "..MaxCount
		end
		
		if CountdownStartTick and GetCountdown() > 0 then
			SurfaceUI.Count.Text = #Queue.." / "..MaxCount.."\n"..GetCountdown()
		end
		
		if #Queue >= 0 and #Queue < MaxCount then
			Highlight.FillColor = Color3.fromRGB(0, 255, 0)
		elseif #Queue == MaxCount then
			print(Highlight.Parent.Parent)
			
			Highlight.FillColor = Color3.fromRGB(255, 0, 0)
		end
		
		if CountdownStartTick and GetCountdown() == 0 then
			SurfaceUI.Count.Text = "Teleporting..."
			
			Highlight.FillColor = Color3.fromRGB(255, 0, 0)
			
			local TeleportOptions = Instance.new("TeleportOptions")
			
			local Server = TeleportService:ReserveServer(12153090763)
			
			TeleportOptions.ReservedServerAccessCode = Server
			
			TeleportOptions:SetTeleportData({
				PlayerAmount = #Queue
			})
			
			TeleportService:TeleportAsync(12153090763, Queue, TeleportOptions)
			
			Queue = {}
			
			task.wait(5)
		end
		
		task.wait()
		
		self.Debounce = false
	end, ["Debounce"] = false})
end

for _, GameInstance in pairs(workspace.Games:GetChildren()) do
	CreateGameInstance(GameInstance)
end

while true do
	for _, GameInstance in pairs(GameInstances) do
		if GameInstance.Debounce == false then
			coroutine.wrap(GameInstance.Run)(GameInstance)
		end
	end
	
	task.wait()
end

When the elevator is full my output is:

14:14:20.832  Game2  -  Server - GameHandler:70
14:14:20.849  Game2  -  Server - GameHandler:70
14:14:20.866  Game2  -  Server - GameHandler:70
14:14:20.883  Game2  -  Server - GameHandler:70
14:14:20.900  Game2  -  Server - GameHandler:70
14:14:20.915  Game2  -  Server - GameHandler:70
14:14:20.934  Game2  -  Server - GameHandler:70
14:14:20.949  Game2  -  Server - GameHandler:70

and so on..

I hope someone can help me because I have no idea

Highlights are very annoying to work with, I reckon your problem is related to this snippet that can be found here* and (pictured) here.

Whenever I crossed the limit myself, the Highlights bounced back and forth from different objects, similar to what is happening to you. For peak performance optimization, I’d recommend object pooling, which is something you can look into with a quick search.

*link correction

I only have 4 “elevators” at the moment. And I don’t think I need to optimize my performance right now because of my performance is at peak right now. However thank you for effort.

1 Like