Seranok
|
  |
| Joined: 12 Dec 2009 |
| Total Posts: 11083 |
|
|
| 12 Feb 2015 06:49 PM |
http://wiki.roblox.com/index.php?title=Common_Code_Snippets
I'm trying to compile a list of frequently used functions. Please post your suggestions here so I can add them to the wiki article. |
|
|
| Report Abuse |
|
|
|
| 12 Feb 2015 06:51 PM |
| I keep thinking that I need a big list of the most asked questions, but I never get around to it. :( |
|
|
| Report Abuse |
|
|
|
| 12 Feb 2015 06:52 PM |
You should add a page about players characters and how to access them it confuses some new people
|
|
|
| Report Abuse |
|
|
Seranok
|
  |
| Joined: 12 Dec 2009 |
| Total Posts: 11083 |
|
|
| 12 Feb 2015 06:58 PM |
ChiefDelta: What exactly should I add that's not covered here?
http://wiki.roblox.com/index.php?title=API:Class/Player/Character |
|
|
| Report Abuse |
|
|
eLunate
|
  |
| Joined: 29 Jul 2014 |
| Total Posts: 13268 |
|
|
| 12 Feb 2015 06:58 PM |
for i=0,1,.1 do part.Transparency = i; wait(); end
D'you ever do that? |
|
|
| Report Abuse |
|
|
|
| 12 Feb 2015 06:58 PM |
what are those things called again
something like
function Scan(twannywan) if twannywan.Name == '21' then twannywan.Name = 'ustupid' end for i,v in pairs(twannywan:GetChildren()) do Scan(v) end end
Scan(workspace) |
|
|
| Report Abuse |
|
|
amanda
|
  |
| Joined: 21 Nov 2006 |
| Total Posts: 5925 |
|
|
| 12 Feb 2015 06:58 PM |
| Teleporting all players to the same place for a minigame or just any game that has a lobby. I hear that one a lot because it's used in a lot of games. |
|
|
| Report Abuse |
|
|
|
| 12 Feb 2015 07:01 PM |
Seranok:
A lot of people don't understand that a player's character can be found as a descendant of the player, it's confusing because they try referencing the player's character in workspace.
I guess it's fine, but it would be better if the Wiki was organized better.
Also, on the "hat" page it doesn't mention that you can add a hat to a character by parenting it to the character all it says is about welding it.
Also for the code snippet page you should add the function to get the player sitting in a seat. |
|
|
| Report Abuse |
|
|
|
| 12 Feb 2015 07:01 PM |
"what are those things called again
something like"
Recursive functions. |
|
|
| Report Abuse |
|
|
amanda
|
  |
| Joined: 21 Nov 2006 |
| Total Posts: 5925 |
|
|
| 12 Feb 2015 07:02 PM |
| verbalAKspray: Recursive functions? |
|
|
| Report Abuse |
|
|
amanda
|
  |
| Joined: 21 Nov 2006 |
| Total Posts: 5925 |
|
|
| 12 Feb 2015 07:03 PM |
| I'm so mad. Even though the time stamp is a minute that was literally seconds before I clicked post. |
|
|
| Report Abuse |
|
|
|
| 12 Feb 2015 07:03 PM |
Perhaps a shorthand for saving to datastore.
function saveData(k,v) local ds = game:getService'DataStoreService'; ds = ds:getDataStore(game.PlaceId); ds:setAsync(k,v); end
function getData(k) local ds = game:getService'DataStoreService'; ds = ds:getDataStore(game.PlaceId); return ds:getAsync(k); end |
|
|
| Report Abuse |
|
|
amanda
|
  |
| Joined: 21 Nov 2006 |
| Total Posts: 5925 |
|
|
| 12 Feb 2015 07:04 PM |
Perhaps a shorthand for saving to datastore.[2]
I second this, as shown 2 lines previous. |
|
|
| Report Abuse |
|
|
|
| 12 Feb 2015 07:08 PM |
yea recursive functions
i like those |
|
|
| Report Abuse |
|
|
Seranok
|
  |
| Joined: 12 Dec 2009 |
| Total Posts: 11083 |
|
|
| 12 Feb 2015 07:10 PM |
I added one for recursively doing something with an object and all its descendants.
For teleporting to a new server, are you talking about using CreatePlaceAsync? |
|
|
| Report Abuse |
|
|
|
| 12 Feb 2015 07:10 PM |
You could also use this getDescendants function. It was made using 128GB's clever recursion method.
function getDescendants(f) local a,b = f:getChildren(),1; while (a [b]) do local k,v = next(a[b]:getChildren(), nil); while (k) do table.insert(a,v); k,v = next(a[b]:getChildren(), k); b = b + 1; end end return a; end
|
|
|
| Report Abuse |
|
|
cntkillme
|
  |
| Joined: 07 Apr 2008 |
| Total Posts: 44956 |
|
|
| 12 Feb 2015 07:11 PM |
'A lot of people don't understand that a player's character can be found as a descendant of the player, it's confusing because they try referencing the player's character in workspace.' It's not a descendant. |
|
|
| Report Abuse |
|
|
128GB
|
  |
| Joined: 17 Apr 2014 |
| Total Posts: 8056 |
|
|
| 12 Feb 2015 07:14 PM |
local function getDescendants(instance) local output, x = instance:GetChildren(), 1 while (children[x]) do for _, child in next, children[x]:GetChildren() do table.insert(output, child) end end return output; end |
|
|
| Report Abuse |
|
|
Seranok
|
  |
| Joined: 12 Dec 2009 |
| Total Posts: 11083 |
|
|
| 12 Feb 2015 07:15 PM |
KOTwarrior: Making a table of all descendants of an object can be a bad idea, because it could easily be in the thousands. It's probably better to use an iterator.
cntkillme: This is why we need a ROBLOX manual. Something which doesn't assume knowledge of the basics. Most people who don't understand properties also won't be on the wiki anyways. |
|
|
| Report Abuse |
|
|
|
| 12 Feb 2015 07:15 PM |
mr.gb, i think u accidentally referenced the table 'output' as 'children'.
ok |
|
|
| Report Abuse |
|
|
128GB
|
  |
| Joined: 17 Apr 2014 |
| Total Posts: 8056 |
|
|
| 12 Feb 2015 07:17 PM |
local function getDescendants(instance) local output, x = instance:GetChildren(), 1 while (output[x]) do for _, child in next, output[x]:GetChildren() do table.insert(output, child) end end return output; end
|
|
|
| Report Abuse |
|
|
128GB
|
  |
| Joined: 17 Apr 2014 |
| Total Posts: 8056 |
|
|
| 12 Feb 2015 07:18 PM |
>.<
local function getDescendants(instance) local output, x = instance:GetChildren(), 1 while (output[x]) do for _, child in next, output[x]:GetChildren() do table.insert(output, child) end x = (x + 1) end return output; end |
|
|
| Report Abuse |
|
|
128GB
|
  |
| Joined: 17 Apr 2014 |
| Total Posts: 8056 |
|
|
| 12 Feb 2015 07:24 PM |
A second optional argument for getTeamPlayers that lets you get neutral players if you want
local function getTeamPlayers(color, neutral) local output = {} for _, player in next, game.Players:GetPlayers() do table.insert(output, (((player.TeamColor.Name == color) and ((not player.Neutral) or neutral)) and player or nil)) end return output; end
--Or easier to read
local function getTeamPlayers(color, neutral) local output = {} for _, player in next, game.Players:GetPlayers() do if ((player.TeamColor.Name == color) and ((not player.Neutral) or neutral)) then table.insert(output, player) end end return output; end |
|
|
| Report Abuse |
|
|
|
| 12 Feb 2015 07:37 PM |
function ExecuteOnAllPlayers(func) for i,v in pairs(game.Players:GetPlayers()) do func(v) end end
--Usage:
ExecuteOnAllPlayers(function(plr) print(plr.TeamColor) end)
function Timer(W, T, D, func) for i = T, 0, D do func(i) wait(W) end end
--Usage:
Timer(1, 10, 1, function(Curr) print("Intermission: " .. tostring(Curr)) end)
function ExecuteIn(T, func) wait(T) func() end
--Usage:
ExecuteIn(5, function() print("Time Up!") end) |
|
|
| Report Abuse |
|
|
128GB
|
  |
| Joined: 17 Apr 2014 |
| Total Posts: 8056 |
|
|
| 12 Feb 2015 07:44 PM |
| ExecuteIn is very useless lol |
|
|
| Report Abuse |
|
|