Very CRAPPY SYSTEM need help fixing it (How do I make a touch event that can fire for multiple NPCs at the same time)

Provide an overview of:

  • What does the code do and what are you not satisfied with?

What I am not satisfied with is that the touch event only works for 1 NPC at a time-the first one that
touches the part.
Basically 6000 thousands of lines of code plus some more which is all repeated, to achieve for one a
up and down movement and a part touch detector for a specific NPC.

  • What potential improvements have you considered?

I looked through all the Dev forum pages that had relative problems but none had the answer I was
trying to achieve

  • How (specifically) do you want to improve the code?

Putting it frankly I don’t want 120 parts going up and down every 1.5 seconds, it creates lag.
So how about just 1 part that detects all the NPCS

I DONT THINK PROVIDING THE CODE IS NECCECARRY BUT HERE:

DETECTOR CODE: (* 120)

flagcapture1.Touched:Connect(function (part)

local humanoid1 = part.Parent:FindFirstChild("Humanoidr2")


if humanoid1 then
	flagcapture1.CanTouch = false

	if teamBlue == 0 and teamGreen == 0 and teamBlack == 0  then

		if teamRed < 100 then

		
			teamRed += 1
		end

	else
		
		if teamBlue > 0 then
			teamBlue -= 1
		elseif teamGreen > 0 then
			teamGreen -= 1
		elseif teamBlack > 0 then
			teamBlack -= 1
		end


	end

end
wait(1)
flagcapture1.CanTouch = true

end)

UP AND DOWN CODE:(* 120)
local part = script.Parent
local deb = false

while true do

if deb == false then
	deb = true
	wait(1)
	part.Position += Vector3.new(0,5,0)
	wait(0.5)
	part.Position -= Vector3.new(0,5,0)
	deb = false
end
wait()	

end

3 Likes

I’m not sure I entirely understand your issue, but I’ll take a shot.

First off, wait is deprecated and has been replaced with task.wait so you might wanna update your code to that, you can do if not deb then as apposed to the if deb == false then check you were doing.

But at the end of the day, since you’re having issues with your code, this needs to be in the #help-and-feedback:scripting-support category. Move this post over there so we can help!

1 Like

I’m not sure I understand. Does GetTouchingParts() fix your issue?