Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

How do you write events? I'm confused.

Asked by 6 years ago

HOLD UP, I know how to write them but I don't know what a certain part is. For example

game.Players.PlayerAdded:connect(function(players)

I know how to write this but what I don't get is (I'll put an x where I don't get cause I think I know the name of it but not sure so gonna do this, I think its argument)

game.Players.PlayerAdded.connect(function(X)

what is this for? What does it mean, how do I know what to change it to in different circumstances.

I know this question is confusing but please don't remove it.

2 answers

Log in to vote
0
Answered by
cailir 284 Moderation Voter
6 years ago

Hi, BossScorp82103! Your issue is with functions, here is a link to Roblox Wiki of how to use functions: http://wiki.roblox.com/index.php?title=Function

But, the "game.Players.PlayerAdded:connect" is ussed to call a function every time a player connects to your game. Here an example:

game.Players.PlayerAdded:connect(function(players)
    print(players.Name .. " has joined the game!"
end)

This will print on the console the player name plus " has joined the game!".

The word "players" is just a reference to the player that got added.

Good Luck with your game

1
:connect is deprecated plz Programical 653 — 6y
Ad
Log in to vote
0
Answered by 6 years ago

its because lua can get complicated at times

*like how using table + table is not possible unless metamethod __add is found and used correctly*

so lets explain in parts:

game.Players.PlayerAdded this part is to target the event we will be using

:connect() this is a method that actually "connects" the event with our function

now what is inside the () is a single parameter: the function

we could automatically set it to a predefined function:

local function OnTouch(hit)
    hit:GiveLoveAndSupport()
end

Part.Touched:connect(OnTouch)

however, most would rather create a function within the (), to do something like this, we would do this simply with:

(function(parameter, otherparameters) code here end)

and so we do this within :connect()

now parameters within the function() are given to the function by :connect()

i dont exactly know how it would do this, but i could imagine it going like this:

thing:connect =  function(functiontouse)
    functiontouse(thingwegotearlier)
end

and how lua deals with this is that we give parameters to the defined function respectivly:

function foo(a)
    print(a .. "!")
end

doo:connect(foo)


:connect gives out parameters: "doo"

so foo runs like:

foo(doo)

so we would get

doo!

Answer this question