I've written a basic script to test out scope, however when I try to pass player.GetAll() as local getPlayers and call it in a loop it doesn't work (as in when I replace it with player.GetAll() or define the var within the function it'll work
[QUOTE]
local getPlayers = player.GetAll()
local lp = LocalPlayer()
function createWall()
for k, v in pairs( getPlayers ) do
print ("Returned successfully!")
end
end
[/QUOTE]
is there a way to get around this?
This is not really an issue with the scope. The problem is that when the script is loaded and executed (not when the function createWall is executed), the table that player.GetAll() returns might not be what you expect it to be.
You will want to do..
[lua]
local getPlayers = player.GetAll;
local lp = LocalPlayer;
function createWall()
for k,v in pairs(getPlayers())do
print("Returned results.");
end
end
[/lua]
As when the function is called it recalls the player.GetAll function, if you call player.GetAll when the script executes aka when the server starts noone will be on the server.
[QUOTE=zzaacckk;32710480]You will want to do..
[lua]
local getPlayers = player.GetAll;
local lp = LocalPlayer;
function createWall()
for k,v in pairs(getPlayers())do
print("Returned results.");
end
end
[/lua]
As when the function is called it recalls the player.GetAll function, if you call player.GetAll when the script executes aka when the server starts noone will be on the server.[/QUOTE]
You're a huge dumbass, you added a bunch of ;'s into the script and stuffed up line two, it needs a () at the end of LocalPlayer
Anyways, players will not get updated because when you run the script, it saves all the players to a table but will not update it, so instead of saving player.GetAll() to a variable, just use [lua]for _, v in pairs( player.GetAll() ) do[/lua]
[QUOTE]local getAll = player.GetAll
function createWall()
for _, v in pairs( getAll() ) do
print ("Returned successfully!")
end
end[/QUOTE]
worked fine! though, i'm not sure why k was replaced with _!
[QUOTE=Gfoose;32710893]You're a huge dumbass, you added a bunch of ;'s into the script and stuffed up line two, it needs a () at the end of LocalPlayer
Anyways, players will not get updated because when you run the script, it saves all the players to a table but will not update it, so instead of saving player.GetAll() to a variable, just use [lua]for _, v in pairs( player.GetAll() ) do[/lua][/QUOTE]
He's storing the function to a variable, not the table it returns.
[QUOTE=Gfoose;32710893]You're a huge dumbass, you added a bunch of ;'s into the script and stuffed up line two, it needs a () at the end of LocalPlayer
Anyways, players will not get updated because when you run the script, it saves all the players to a table but will not update it, so instead of saving player.GetAll() to a variable, just use [lua]for _, v in pairs( player.GetAll() ) do[/lua][/QUOTE]
getPlayers and lp are references to functions so getPlayers() is the same as player.GetAll().
I put ;'s after every line as it is a habit I adapted from other languages and I don't want to loose it if I take a break from those languages and take a project in lua.
@Arnie _, v and k, v are they same thing, only difference is that _, v is slightly less descriptive as k, v stands for key, value.
[lua]
for _,v in pairs( getAll() )do
[/lua]
and
[lua]
for k,v in pairs( player.GetAll() )do
[/lua]
Both perform the same exact action- to keep it simple I will just say its your preference.
Sorry, you need to Log In to post a reply to this thread.