How to detect individual part being touched server-side with pairs?

I am looking for some help with understanding how to detect a part being touched using the for i, v in pairs function.

I have a code for a racing server that has a function where the track wears down over time and changes color (similar to how a dirt road changes color in real life). I am trying to convert said script from the tires into a server script to help boost server performance.

The script works by having a track with thousands of parts on it, and when they are touched, they change color and lose friction (works fine). I would like to understand how to make it so when the parts are found and detected, they are individually changed when they are touched, not all at once.

If there is a smoother way of doing this besides the way I figured, let me know! Thanks!

If I understand your situation, you’re trying to find a way to loop through all of the parts in the track and detect whether there’s external parts that’s touching that part that’s not of the track. If so, IIRC, you could use Part:GetTouchingParts() for all of the parts within that loop (if that makes sense) to see if the part is being touched.

I’m not sure this is the best way of operation, unless you instead connect to a Part.Touched and run that loop once (So that it don’t create a bunch of function connections), but it should still work.

Something like this:

-- Using Part:GetTouchingParts()
-- Continuously going through the loop and checking
while game:GetService("RunService").Heartbeat:Wait() do
    for i, part in pairs(Track:GetChildren())
    -- if you didn't know:
    -- i = name (pairs) // number/position (ipairs)
    -- part = v = value
        if #part:GetTouchingParts() > 0 then
            -- code
        end
    end
end

-- Using Part.Touched
-- Only need to run the loop once
for i, part in pairs(Track:GetChildren()) do
    part.Touched:Connect(function()
        -- code
    end)
end

It might be more optimal to use the Part.Touched, but that’s just me.

1 Like

I’m just here to suggest improvements for readability:

  • use while true do with a task.wait at the end, it does the same thing but shorter
  • use GetService pls
  • you misspelled the last pairs
  • you don’t need to use pairs anymore, you can just do for i, v in part:GetTouchingParts() do
1 Like

Again, it’s not like I was trying to make the best of the best scripts; it’s just to show how he might be able to script it… But I thank you for the input.

Also, for the last suggestion, I’m not sure where you see me using part:GetTouchingParts() as a loop but, that’s to check if there’s at least one part that’s touching that part in the GetTouchingParts() table.

1 Like

It was just an example :slight_smile: you can do it for any table (i.e. list/array, dictionary).

wait you dont need to use pairs? does it change anything?

1 Like

Most people won’t need to use pairs or ipairs anymore, there’s only really one use case left for ipairs, which is that it stops automatically when it reaches a nil value.

1 Like

Thanks for your response, I will check if that works and let you know.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.