Game starts lagging when a taco gets meat

So I’m making taco machines and am currently in the process of testing the meat dropper but when the first batch of tacos (two) get meat, the game starts lagging A TON. So I’m thinking it’s a problem with the script.

Here it is:

script.Parent.Touched:Connect(function(hit)
	if hit.Name == "Taco" then -- hit is taco
		for i, v in pairs(hit.Parent.Meat:GetChildren()) do
			if v.Name ~= "Part" then
				v.Transparency = 0
			end
			wait()
		end
	elseif hit.Parent.Parent:FindFirstChild("Taco") then -- hit is part of meat
		for i, v in pairs(hit.Parent:GetChildren()) do
			if v.Name ~= "Part" then
				v.Transparency = 0
			end
			wait()
		end
	end
end)

And here is a view of the explorer (a taco):


All the meat parts (called “Union”) are connected to the part called “Part” and that part is connected to the taco shell.

Do you know why this is happening? How can I make it not lag?

I think unions are yeilded so I would add a wait() after GetChildren()) do

1 Like

I already have a wait() there, and putting it before the if statement doesn’t help.

Oh I misread. Is there any children in the unions causing it to lag?

Maybe get rid of the wait? it’s unnecessary.

I put the wait because it was lagging. The wait didn’t even help :frowning:

@Bird_7x only WeldContraints, I don’t think it’s a big deal.

Debounce the function as well. It’s gonna spam up a bunch because of how touched works.

2 Likes

wait(1)?

wait(1) is too slow but I’ll try it out.

@pobammer I did the debounce when you said it :neutral_face:

wait(1) didn’t do anything also.


I didn’t tell you this, but it actually starts lagging when the taco falls off the conveyor.

That’s not the thing you want to do. The debounce suggestion is what you should be doing.

Try making this:

elseif hit.Parent.Parent:FindFirstChild("Taco") then -- hit is part of meat
		for i, v in pairs(hit.Parent:GetChildren()) do
			if v.Name ~= "Part" then
				v.Transparency = 0
			end
			wait()
		end
	end

Look like this:

elseif hit.Parent and hit.Parent.Parent and hit.Parent.Parent.Name == "Taco" then -- hit is part of meat
		for i, v in pairs(hit.Parent:GetChildren()) do
			if v.Name ~= "Part" then
				v.Transparency = 0
			end
		end
	end

But the thing is, the parent’s parent name is not Taco…

So that wouldn’t even go that far.


That’s why you use debounce like @pobammer said.

1 Like

OH I KNOW WHY. When you use Part.Touched it fires multiple times! So if keeps firing until not touched.

1 Like

I don’t think you misunderstand.

I’m talking about the circled taco, not the taco model.

I can make it a mesh though.


To the reply below:
Screen Shot 2020-06-20 at 10.24.56 PM
A meat dropper :smiley:

Actually, i have a question, could you show a image of the explorer on where the code you shown belongs to.

Edit: And i did misunderstand…

1 Like

I tried to reproduce and fix your case in an empty place, feel free to testrun it.
tacomeat.rbxl (19.1 KB)

local FallingMeat = script.Parent
local Taco
local TouchedConnection

function onTacoModelTouched(hit)
	if hit.Name == "Taco" then -- hit is taco
		Taco = hit
		for _, v in pairs(Taco.Parent.Meat:GetChildren()) do
			if v:IsA("BasePart") or v:IsA("UnionOperation") or v:IsA("MeshPart") then
				v.Transparency = 0
			end
		end
	elseif hit.Parent.Parent:FindFirstChild("Taco") then -- hit is part of meat
		Taco = hit.Parent.Parent.Taco
		for _, v in pairs(Taco:GetChildren()) do
			if v:IsA("BasePart") or v:IsA("UnionOperation") or v:IsA("MeshPart") then
				v.Transparency = 0
			end
		end
	end
	--print("done")
	Touchedconnection:Disconnect()
end

Touchedconnection = FallingMeat.Touched:Connect(onTacoModelTouched)

Your current code creates lag because the Touched event fires for every miniscule movement a part makes against another part, even if it has already changed the intended set of parts’ transparency. Disconnecting this event would be useful for preventing lag caused by that.

I didn’t want to change things too much since I don’t know all the details of your setup, but this should work.

1 Like
local taco
local connection

connection = script.Parent.Touched:Connect(function(hit)
	if hit.Name == "Taco" then -- hit is taco
		taco = hit.Parent
		for i, v in pairs(taco.Meat:GetChildren()) do
			wait()
			if v.Name ~= "Part" then
				v.Transparency = 0
			end
		end
	elseif hit.Parent.Parent:FindFirstChild("Taco") then -- hit is part of meat
		taco = hit.Parent.Parent
		for i, v in pairs(taco.Meat:GetChildren()) do
			wait()
			if v.Name ~= "Part" then
				v.Transparency = 0
			end
		end
	end
	print("Finished")
	connection:Disconnect()
end)

Somehow this didn’t do anything, even with

I got rid of the debounce but now I’m thinking it should be put back, because “Finished” comes in the output 52 times with only two tacos.

Also if I get rid of wait() then only one taco gets meat…

Are those meat models made out of a lot of parts? Maybe your computer is too slow to handle that much.

Just like 5 parts unioned together.

If the unions turn out to be the antagonist, then I will make it into a mesh :slight_smile:

I have found out that the unions are ONE of the antagonists. I have no idea why, since the unions are now meshes, it’s not laggy anymore but this happens:


I think the other antagonist is this script located in the taco shell dropper:

local tacos = script.Parent.Tacos
local tacoFolder = workspace.Tacos

local tweenServ = game:GetService("TweenService")
local tInfo = TweenInfo.new(1,Enum.EasingStyle.Quint,Enum.EasingDirection.Out,0,false,0)

while true do
	wait(2)
	--getting tacos coming out
	local nextTacos = {}
	local tacoTable = {}
	for i, v in pairs(tacos:GetChildren()) do
		if v.Name == "NextTaco" then
			table.insert(nextTacos, v)
		elseif v.Name == "Taco" then
			tacoTable[v] = v.Taco.Position
		end
		local newTween = tweenServ:Create(v.Taco, tInfo, {
			CFrame = v.Taco.CFrame + Vector3.new(2, 0, 0)
		})
		newTween:Play()
	end
	wait(1.5)
	for _, nextTaco in pairs(nextTacos) do
		nextTaco.Taco.Anchored = false
		nextTaco.Parent = tacoFolder
		nextTaco.Name = "IAmPinleon's Taco"
		nextTacos = {}
	end
	wait(.5)
	for i, v in pairs(tacoTable) do
		local newTaco = i:Clone()
		newTaco.Parent =  tacos
		newTaco.Taco.Position = v
		
		i.Name = "NextTaco"
	end
end

I think the tween or making it unanchored has to do something with it.