Multiple things to return from one function

What I want

I want a touch function that will return both the player that touched it, and the part that touched it. This is because I have a thing I made that needs to know if the correct tool hit it while still needing the players name.

My Example Script

script.Parent.Touched:Connect(function(part, player)
print(player.." has touched "..part)
end

If you just want to find the player that touched the part, you can use this:

local function GetPlayerFromHit(hit)
    local player

    if hit.Parent:FindFirstChild("Humanoid") then
        player = game.Players:GetPlayerFromCharacter(hit.Parent)
    end

    return player
end

script.Parent.Touched:Connect(function(hit)
    local player = GetPlayerFromHit(hit)
end)

edit: I mistyped something, but this should be fine now

1 Like

I want both the part and the actual player

The part is the variable hit and the player is the variable player

1 Like

Ohhh that is so smart thank you

When using it though, make sure that you check if the player exists, because it will return nil if it wasn’t a player that touched it

Local function idk()
Local x = 1
Local b = 3
Return x, b
End

Local x,b = idk()

That’s how u return multiple values, I typed this on a phone btw