When you connect a function to an event, the value(s) provided by the event (i.e. PlayerAdded provides the player that joined) are passed to the function connected to it.
game.Players.PlayerAdded:Connect(function(NewPlayer)
print(NewPlayer.Name, "joined the game!")
end)
When an event is Connected, it returns a RBXScriptConnection (roblox.com). A RBXScriptConnection has a Connected boolean property to it and a Disconnect method for it. The former determines whether the connection is still Connected whilst the latter Disconnects the connection.
local TouchedConn = nil
TouchedConn = BasePart.Touched:Connect(function(Hit)
print("Touched connection is still connected:", TouchedConn.Connected) -- touched connection is still connected: true
TouchedConn:Disconnect()
print("Touched connection is still connected:", TouchedConn.Connected) -- touched connection is still connected: false
print("The last thing to step on me was", Hit.Name, "!")
print("I only outputted once!")
end)
The solution I supplied earlier utilizes this. Unlike Connection:Wait() which yields the thread, Connect does not and allows the flow of the thread to continue. In the code I supplied, after MoveToFinished fires, it Disconnects it and runs the MoveTo code, allows the code to do what you want yet having the flow of the entire script to continue.
Using Connection:Wait():
local function NewPart()
local Part = Instance.new("Part", game.Workspace)
Part.Touched:Wait()
print("I outputted only once!")
end
NewPart()
*player steps on*
-- I outputted only once!
NewPart()
-- etc
NewPart()
-- etc
-- And to have it output all at once:
coroutine.wrap(NewPart)()
coroutine.wrap(NewPart)()
coroutine.wrap(NewPart)()
-- I outputted only once!
-- I outputted only once!
-- I outputted only once!
Utilizing Connection:Disconnect:
local function NewPart()
local TouchedConn = nil
local Part = Instance.new("Part", game.Workspace)
TouchedConn = Part.Touched:Connect(function()
TouchedConn:Disconnect()
print("I outputted only once!")
end)
end
NewPart()
NewPart()
NewPart()
-- I outputted only once!
-- I outputted only once!
-- I outputted only once!
-- Doesn't require the need of a coroutine since they aren't yielding the thread/script
Needless to say, the latter is the solution I recommended and what would happen.