I was just playing around and I have two questions using this script.
function round(player) game.Players.PlayerAdded:connect(function(p) end) end
1) Using this script, will this use "p" as a variable for every player that enters (meaning I could access all the players using p)
2) Is it possible for me to access it after other functions have run as long as I don't define p anywhere else. Meaning I could use "p" in a separate function.
Hope this makes sense. Thanks
1) The 'p' variable there can be used for every player that joined the game AFTER the function is executed. If you want to get all players in the current server, use a for loop with the GetPlayers method.
2) No. Any variables within any function are only accessible there. You would have to re-define them in a separate function.
1) p
is a parameter to your function. The function is reevaluated with a new (set of) parameter(s) each time the function is called. Each time a player joins, it gets called with a the new player.
But it's only one. Variables can only have one single value. Those values could be lists, but they cannot take on multiple values.
In this case, they are all separate -- just separate calls to the same function (think, like cosine
; cosine(5)
is in its own vacuum)
2) No. Parameters are automatically local
to that function. However, you can set a different global variable:
lastCalled = nil function func(x) lastCalled = x end func(5) print(lastCalled, x) -- 5 nil func(4) print(lastCalled,x) -- 4 nil print(lastCalled,x) -- 4 nil
While we can't get x
out, func
can write to lastCalled
, which we can get afterwards.
function round(player) game.Players.PlayerAdded:connect(function(p) people_on = p end) end function test() people_on.Character.Humanoid.Health = 0