✓ Accepted answer
Ok, so that has some errors. First of all, why did you make a function inside of another function? I mean, if you make something that's going to be used multiple times within the .Touched, it would be useful to make the function, but since you only use that function once, and that's the only thing you use in the .Touched, there is no reason to make another function.
part = script.Parent
part.Touched:connect(function(hit)
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
if player then
local players = game.Players:GetPlayers()
local num = math.random(1,#players)
while players[num]==player do
num = math.random(1,#players)
end
local otherPlayer = players[num]
torso1 = player.Character.Torso
torso2 = otherPlayer.Character.Torso
torso1.CFrame = torso2.CFrame
end
end)
If you find anything wrong with this, please tell me in a comment.
And here's an example of when it would be a great idea to make a function inside of another function.
function thing()
local function randParts(num)
local parts = {}
local childs = game.Workspace:GetChildren()
for q=1,#childs do
if childs[q]:IsA("BasePart") then
table.insert(parts,#parts+1,childs[q])
if #parts ==num then
break
end
end
end
return parts
end
local p1 = randParts(1)[1]
p1:Destroy()
local tab = randParts(10)
for q=1,#tab do
local decide = math.random(1,2)
if decide == 1 then
tab[q]:Destroy()
else
tab[q].BrickColor = BrickColor.Blue()
end
end
end