Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by
enes223 327 Moderation Voter
5 years ago
Edited 5 years ago

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
0
what are you trying to do? create a touch event for each of the blocks? royaltoe 5144 — 5y

1 answer

Log in to vote
1
Answered by
royaltoe 5144 Moderation Voter Community Moderator
5 years ago

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
0
works like a charm, thx! enes223 327 — 5y
0
np buddy !!!!!!!!!! royaltoe 5144 — 5y
Ad

Answer this question