How can I optimize this marker system?

idk if optimize is the right word lol, but so I just coded this thing for my game and honestly, I struggled with it and barely got it to work and the only issue is that it causes a LOT of lag. Basically though when a players mouse targets an enemy then I just wanna put this triangle-shaped part over the targets head and then if they move off of it then I wanna remove it. I think I had the right idea, but I just didn’t execute it properly smh


mouse.Move:Connect(function()
	if deb == true then
		deb = false
		if mouse.Target ~= nil and workspace.Enemies:FindFirstChild(mouse.Target.Parent.Name) then
			if isAttacking == true then
				if createMarker == true then
					createMarker = false
					--print("TEST 1")
					local hover = workspace.Enemies:FindFirstChild(mouse.Target.Parent.Name)
					if not workspace.Temp:FindFirstChild("Marker") then
						--print("TEST 2")
						local marker = repStorage.Models.Marker
						local markerClone = marker:Clone()
						markerClone.Position = hover.Head.Position + Vector3.new(0,3,0)
						markerClone.Parent = workspace.Temp														
					end
				end
			end
		else
			if workspace.Temp:FindFirstChild("Marker") then
				workspace.Temp:FindFirstChild("Marker"):Destroy()
				createMarker = true	
			end
		end
		deb = true
	end
end)

For starters, can you change the indenting so your code isn’t all the way to the right hand side of the screen? Sort of annoying to scroll all the way over. Trivial concern but your code shouldn’t be indented all the way out there when posting to the forum (but in your actual codebase, might be necessary).

Oh sorry I just copied and pasted it from my script lol it’s in a long piece of code which is why it’s like that I edited the post though and fixed it :s

not sure if this will help. But can’t you use collection service? Its basically a service where you can mark objects with tags.

https://developer.roblox.com/en-us/api-reference/class/CollectionService

Hmm I don’t think that’s gonna help with what I’m trying to do lol here’s a vide of what the code does it places the red shape above the head of the mouse target however as you can see it’s hella laggy and the activity in the script performance jumps to like 80 which is uh not good I’d imagine (sorry for the music btw might b e a bit loud)

Then have it so when you click on them the red arrow comes up. With the “Are you sure?” Gui. Hence you give the player more options to think and it saves optimisation.