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

Questions with variables?

Asked by
wjs3456 90
10 years ago

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

3 answers

Log in to vote
1
Answered by
Shawnyg 4330 Trusted Badge of Merit Snack Break Moderation Voter Community Moderator
10 years ago

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.

0
If have done for i,v in pairs(game.Players:GetPlayers()) do before but for some reason what I want it to do only happens to the first person who joins wjs3456 90 — 10y
Ad
Log in to vote
1
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
10 years ago

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.

0
Firstly what does the function above do. Second is there a way for me to put a function with PlayerAdded not first(like in the middle of the script) I assumed it would either break it or just count people who joined after. wjs3456 90 — 10y
0
Or perhaps could I do this? wjs3456 90 — 10y
0
So how could I use that doing this script. I don't understand what the variable would be. wjs3456 90 — 10y
0
Oh ok. Is there a way to use your script to fix mine? wjs3456 90 — 10y
View all comments (3 more)
0
I don't know what it is that you are trying to accomplish, you haven't said that. I think you're probably pursuing the wrong direction, wherever you're trying to go, so it would be better to actually state what you're trying to do BlueTaslem 18071 — 10y
0
Yeah I'm sorry. Basically what I am attempting to do is change all the players TeamColor to blue. But when I use i,v in pairs(game.Players:GetPlayers()) it only works for the first person who joins. wjs3456 90 — 10y
0
Doing a loop over all players is the correct solution. What you have written is correct. BlueTaslem 18071 — 10y
Log in to vote
0
Answered by
wjs3456 90
10 years ago
function round(player)
game.Players.PlayerAdded:connect(function(p)
people_on = p
end)
end

function test()
people_on.Character.Humanoid.Health = 0
0
`new()` would kill the last (most recent) player to join. BlueTaslem 18071 — 10y

Answer this question