When I look at scripts, I see something say :Connect
What does it mean? Why do you need to use it in Roblox Studio? Why is it useful? I want to know why.
If you have the solution, Thank you!
Sincerely, Ducksneedhelp
:Connect
'connects' a function to an event, so when the event happens, the function is triggered. For example, look through this script:
local function yeet(hit) --Function that prints the part that hit print(hit) end script.Parent.Touched:Connect(yeet) --[[Connects function to Touched event, so the function will run every time the part is hit]]--
:Connect
can be used with other events as well. More info can be found on the Roblox Wiki:
https://developer.roblox.com/en-us/articles/events
I'd like to say that connect is a function of events, but I don't know if that's true. I'd also like to say that events are properties of whatever they are a part of, but that's not entirely true either. Events are their own thing. One event of Players is Players.PlayerAdded. When it's fired, a.k.a. someone joins, it's fired with the player object.
Players.PlayerAdded:Connect(function(player) end)
lets you work with every player that will be in your game. It's also worth noting that these events firing won't yield anything in your script. They run on their own, meaning you can have infinite loops both under the function for PlayerAdded and in other parts of the script, both running at the same time. You can also achieve this affect using the spawn function