Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Questions with variables?

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

  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 comment

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

  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)


  1. 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.

7 comments

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.

0

Or perhaps could I do this?

0

So how could I use that doing this script. I don't understand what the variable would be.

0

Oh ok. Is there a way to use your script to fix mine?

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

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.

0

Doing a loop over all players is the correct solution. What you have written is correct.

function round(player)
game.Players.PlayerAdded:connect(function(p)
people_on = p
end)
end

function test()
people_on.Character.Humanoid.Health = 0

1 comment

0

new() would kill the last (most recent) player to join.

This discussion is preserved as it was. New answers, comments, and votes are closed.