Trying to detect if a player touched a part that's stored within a table [Solved]

My main objective:
I want to see if its possible to detect if a player collided with a part within a table, Rather than using individual scripts to detect if a player touched a part.

local test = {
	game.Workspace.Platform_1,
	game.Workspace.Platform_2,
	game.Workspace.Platform_3,
	game.Workspace.Platform_4
}

So with these different parts in the table I’d want to check if a player touched a part in it, Any ideas on how I could do that.

I don’t have much experience with tables so just need some help figuring it out.

What you could do is do this

local test = {
	game.Workspace.Platform_1,
	game.Workspace.Platform_2,
	game.Workspace.Platform_3,
	game.Workspace.Platform_4
}

game.Workspace.SpawnLocation.Touched:Connect(function(Hit)
	if table.find(test, game.Workspace.Platform_1) and Hit.Parent:FindFirstChild("Humanoid") then
	
	end
end)

If you would like to do it automatically then you could loop through the table

local test = {
	game.Workspace.Platform_1,
	game.Workspace.Platform_2,
	game.Workspace.Platform_3,
	game.Workspace.Platform_4
}

for i, Part in pairs(test) do
	Part.Touched:Connect(function(Hit)
		if Hit.Parent:FindFirstChild("Humanoid") then
			
		end
	end)
end
1 Like

We can iterate over the table:

local test = {
	game.Workspace.Platform_1,
	game.Workspace.Platform_2,
	game.Workspace.Platform_3,
	game.Workspace.Platform_4
}

for i, testPart in pairs(test) do
    print(i, testPart) -- Not needed, but will be useful for you to see what this for loop is doing :)
    testPart.Touched:Connect(function(touchPart)
        -- Logic to see if `touchPart` is from a players' character
    end)
end
1 Like

That was a possibility I was thinking, but wouldn’t that be difficult to go through every single part just to find the one specific part the player is touching upon.

I was wrong, looks like your script actually does work and can detect if a player touches a part. Thanks alot for this