Can´t highlight all enemies

  1. What do you want to achieve?
    I am trying to highlight ALL enemies after pressing a button on the keyboard.

  2. What is the issue? Include screenshots / videos if possible!
    Here is a video of how it currently “works”:


    After button press it will highlight one enemy and after some time the other enemy will be highlighted.

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    Yes, I have tried to find something similar to this problem on Forum. I also tried YouTube, but nothing new.

  4. Here you can see the current script.

local Players = game:GetService("Players")
local UserInputService = game:GetService("UserInputService")
local cc = game:GetService("CollectionService")
ready = true

local function OnKeyPress(inputObject)
	if inputObject.KeyCode == Enum.KeyCode.X and ready == true then
		local function EnemyFunc(part)
			ready = false
			script.ShowSound:Play()
			local NewHigh = Instance.new("Highlight")
			NewHigh.OutlineColor = Color3.fromRGB(255, 0, 0)
			NewHigh.FillTransparency = 0.5
			NewHigh.FillColor = Color3.fromRGB(255, 0, 0)
			NewHigh.DepthMode = Enum.HighlightDepthMode.AlwaysOnTop
			NewHigh.Parent = part
			task.wait(3.5)
			NewHigh:Destroy()
			script.HideSound:Play()
			ready = true
		end

		for _,v in pairs(cc:GetTagged("HighlightEnemy")) do
			EnemyFunc(v)
		end
	end
end	

UserInputService.InputBegan:Connect(OnKeyPress)

And yes, both enemies are Tagged with “HighlightEnemy”

Thank you for your ideas and help!

1 Like

Edit: Oh wait theres a task.wait in the for loop you will need task.spawn to avoid yielding the thread.

local Players = game:GetService("Players")
local UserInputService = game:GetService("UserInputService")
local cc = game:GetService("CollectionService")
ready = true

local function OnKeyPress(inputObject)
	if inputObject.KeyCode == Enum.KeyCode.X and ready == true then
		local function EnemyFunc(part)
			ready = false
			script.ShowSound:Play()
			local NewHigh = Instance.new("Highlight")
			NewHigh.OutlineColor = Color3.fromRGB(255, 0, 0)
			NewHigh.FillTransparency = 0.5
			NewHigh.FillColor = Color3.fromRGB(255, 0, 0)
			NewHigh.DepthMode = Enum.HighlightDepthMode.AlwaysOnTop
			NewHigh.Parent = part
			task.wait(3.5)
			NewHigh:Destroy()
			script.HideSound:Play()
			ready = true
		end

		for _,v in pairs(cc:GetTagged("HighlightEnemy")) do
task.spawn(function()
			EnemyFunc(v)
end)
		end
	end
end	

UserInputService.InputBegan:Connect(OnKeyPress)

Code looks alright, are you aware of the highlight number limitation mentioned in the documentation?

As a performance limit, Studio only displays 31 simultaneous Highlight instances on the client at a time.

An idea for working around this limit is by highlighting the enemies positioned in front of the camera using world to screen point with the depth number Z. If this number is positive that means the enemies are in front of the camera and need to be highlighted.

2 Likes

Yes, I am aware of the highlight limit.
And the spawn function fixed the problem XD. I’m still kinda getting into scripting.

Thank you.

2 Likes
local Players = game:GetService("Players")
local UserInputService = game:GetService("UserInputService")
local cc = game:GetService("CollectionService")
ready = true

local function EnemyFunc(part: BasePart)
	ready = false
	script.ShowSound:Play()
	local NewHigh = Instance.new("Highlight")
	NewHigh.OutlineColor = Color3.fromRGB(255, 0, 0)
	NewHigh.FillTransparency = 0.5
	NewHigh.FillColor = Color3.fromRGB(255, 0, 0)
	NewHigh.DepthMode = Enum.HighlightDepthMode.AlwaysOnTop
	NewHigh.Parent = part
	task.wait(3.5)
	NewHigh:Destroy()
	script.HideSound:Play()
	ready = true
end

local function OnKeyPress(inputObject, gpe)
	if gpe then return end
	if inputObject.KeyCode == Enum.KeyCode.X and ready then
		for _, v in pairs(cc:GetTagged("HighlightEnemy")) do
			task.spawn(EnemyFunc, v)
		end
	end
end	

UserInputService.InputBegan:Connect(OnKeyPress)

this code is better and cleaner

1 Like

The problem is every time it gets into a loop and highlights an enemy, it highlights, waits, destroys the highlight and only then it moves on to the next enemy.
To solve that, seperate the highlight into 2 functions:

  • One the creates the highlight
  • One that destroys the highlight

And then you can do something like:

for _,v in pairs(cc:GetTagged(“HighlightEnemy”)) do
CreateHighlight(v)
end
wait(3.5)
for _,v in pairs(cc:GetTagged(“HighlightEnemy”)) do
DestroyHighlight(v)
end

2 Likes

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