Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How to call touched event without getting lag with loops like i,v in pairs do?

Im trying to code a spleef but All of part's are same name and I dont know how to code like that and If I do loop like i,v in pairs it lags really badly and I need help :C (laggy means that it doesnt debounce)

script:

--By enes223

local touchedParts = {}

while wait() do
	spawn(function()
		for _,v in pairs(script.Parent:GetChildren()) do
			if not touchedParts[v] then
				touchedParts[v] = true
				v.Touched:connect(function()
					for i = 0.1,1,0.1 do
						v.Transparency = i
						wait(0.01)
					end
					v.CanCollide = false
				end)
			end
		end
	end)
end

1 comment

0

what are you trying to do? create a touch event for each of the blocks?

1 answer

✓ Accepted answer

You can create click events for each of your bricks by doing the following: You only have to run this at the beginning of your game because you only need one click event for each block.

What the script does is gets the blocks in a group and puts them into a table. (I think we went over tables a couple weeks ago, but if you need that explained again, let me know)

It loops through the blocks table and then creates a touch event for each block. Your logic should be where the comment is.

blocks = game.Workspace.BlocksGroup:GetChildren()

for i, v in ipairs(blocks)do
	v.Touched:Connect(function()
		--do the logic for each touch event here
	end)
end

2 comments

0

works like a charm, thx!

0

np buddy !!!!!!!!!!

This discussion is preserved as it was. New answers, comments, and votes are closed.