generic image
Processing...
  • Games
  • Catalog
  • Develop
  • Robux
  • Search in Players
  • Search in Games
  • Search in Catalog
  • Search in Groups
  • Search in Library
  • Log In
  • Sign Up
  • Games
  • Catalog
  • Develop
  • Robux
   
ROBLOX Forum » Game Creation and Development » Scripters
Home Search
 

Re: Common Code Snippets

Previous Thread :: Next Thread 
Seranok is not online. 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
JarodOfOrbiter is not online. JarodOfOrbiter
Joined: 17 Feb 2011
Total Posts: 20029
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
ChiefDelta is not online. ChiefDelta
Joined: 05 Nov 2010
Total Posts: 13071
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 is not online. 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 is not online. 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
verbalAKspray is not online. verbalAKspray
Joined: 09 Jul 2014
Total Posts: 23685
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 is not online. 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
ChiefDelta is not online. ChiefDelta
Joined: 05 Nov 2010
Total Posts: 13071
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
ChiefDelta is not online. ChiefDelta
Joined: 05 Nov 2010
Total Posts: 13071
12 Feb 2015 07:01 PM
"what are those things called again

something like"

Recursive functions.
Report Abuse
amanda is not online. amanda
Joined: 21 Nov 2006
Total Posts: 5925
12 Feb 2015 07:02 PM
verbalAKspray: Recursive functions?
Report Abuse
amanda is not online. 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
KOTwarrior is not online. KOTwarrior
Joined: 13 Jun 2012
Total Posts: 4376
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 is not online. 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
verbalAKspray is not online. verbalAKspray
Joined: 09 Jul 2014
Total Posts: 23685
12 Feb 2015 07:08 PM
yea recursive functions

i like those
Report Abuse
Seranok is not online. 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
KOTwarrior is not online. KOTwarrior
Joined: 13 Jun 2012
Total Posts: 4376
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 is not online. 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 is not online. 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 is not online. 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
KOTwarrior is not online. KOTwarrior
Joined: 13 Jun 2012
Total Posts: 4376
12 Feb 2015 07:15 PM
mr.gb, i think u accidentally referenced the table 'output' as 'children'.

ok
Report Abuse
128GB is not online. 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 is not online. 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 is not online. 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
warspyking is not online. warspyking
Joined: 15 Nov 2011
Total Posts: 13947
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 is not online. 128GB
Joined: 17 Apr 2014
Total Posts: 8056
12 Feb 2015 07:44 PM
ExecuteIn is very useless lol
Report Abuse
Previous Thread :: Next Thread 
Page 1 of 1
 
 
ROBLOX Forum » Game Creation and Development » Scripters
   
 
   
  • About Us
  • Jobs
  • Blog
  • Parents
  • Help
  • Terms
  • Privacy

©2017 Roblox Corporation. Roblox, the Roblox logo, Robux, Bloxy, and Powering Imagination are among our registered and unregistered trademarks in the U.S. and other countries.



Progress
Starting Roblox...
Connecting to Players...
R R

Roblox is now loading. Get ready to play!

R R

You're moments away from getting into the game!

Click here for help

Check Remember my choice and click Launch Application in the dialog box above to join games faster in the future!

Gameplay sponsored by:
Loading 0% - Starting game...
Get more with Builders Club! Join Builders Club
Choose Your Avatar
I have an account
generic image