ROBLOX:在 Touched 函数中获取处理程序对象

ROBLOX: get a handler object in Touched function

我有一张带有 2 个门(蓝色和红色)和一个球的简单地图。

我需要一个侦听器函数来检查球与门的碰撞。

我已经创建了一个服务器脚本:

function goal(hitter)
    if hitter.Name == "ball" then
        print(Instance.Parent)
        print(hitter)
        print("============")       
    end
end

game.Workspace.gate_blue.Touched:Connect(goal)
game.Workspace.gate_red.Touched:Connect(goal)

我需要在函数中检测哪个门被击中了。

如何在函数中获取门名称?

重用 goal 功能的一种方法是创建 higher-order function :

function onGoalTouched(goalName)
    return function(hitter)
        if hitter.Name == "ball" then
            print(Instance.Parent)
            print(hitter, goalName)
            print("============")       
        end
    end
end

local blueGate = game.Workspace.gate_blue
local redGate = game.Workspace.gate_red

blueGate.Touched:Connect(onGoalTouched(blueGate))
redGate.Touched:Connect(onGoalTouched(redGate))