Common functions: Difference between revisions

From Legacy Roblox Wiki
Jump to navigationJump to search
>Mindraker
move list from depth guide -> separate page.
>Anaminus
Redirected page to Function Dump
 
(66 intermediate revisions by 15 users not shown)
Line 1: Line 1:
__TOC__
#redirect [[Function Dump]]
 
==Built-In Functions==
There are many pre-made functions in the Object Browser, but this page will be going through the most-used ones.  Most of these functions either use a pre-existing brick or delay something from happening.  A final note before you start, all built-in functions start with a ":" and end with a "()".
 
===The "wait()" and "delay()" Function===
The wait function will delay, or wait, by seconds.  Unlike other functions, this one can both be with or without a ":".
 
Example:
<pre>
print("Hi!")
wait(3)
print("Bye!")
</pre>
 
In the output, this prints the word "Hi!", waits three seconds, then prints the word "Bye!".
 
See [[http://wiki.roblox.com/index.php?title=Scripting#Globals]] for more details on these functions
 
===The ":Clone()" Function===
What this function does it what it says, clones. When the object is cloned, it will have no parent, so the clone won't exist until you name its new parent. Here's how you do it.
<pre>game.Workspace.Brick:Clone().Parent = game.Workspace</pre>
If you are creating a script that makes Part that has a decal on it, the easier thing to do is to clone the brick, define it's parent, and then it's postion (Warning: The default posion is '''0,0,0'''. If you do not define a cloned part's postion, it will be there!).
 
===The ":FindFirstChild()" Function===
What this function does is find the first object it finds with the name noted in the parantheses. This is very useful in finding if something is there or not.
Example:
<pre>
bin = script.Parent
 
function onTouched(part)
local h = part.Parent:FindFirstChild("Humanoid")
if h~=nil then
print("Hi There!")
end
end
 
bin.Touched:connect(onTouched)
</pre>
If I were to do this without the ":FindFirstChild()", I would come out with a Syntax Error because the object isn't there. But with ":FindFirstChild()", you can ask if the object is nil or not, noted in the script.  The rest of the script you should already know.  If you are trying to find something by name, it is advised to use quotation marks.
 
===The ":GetChildren()" Function===
This function gets a read-only table of the object's children.  You don't really need to use it unless you are using a 'for' statement.
Example:
<pre>
bricks = 0
local c = game.Workspace:GetChildren()
for i=1, #c do
if c[i].className == "Part" then
bricks = bricks + 1
end
end
print(bricks)
</pre>
 
===The ":Remove()" Function===
All the Remove function does is change its parent status to [[nil]].  You could always just use "object.Parent = nil" instead.  But if you want to use this way, fine then.
Example:
<pre>game.Workspace.Brick:Remove()</pre>
 
 
===The ":MakeJoints()" Function===
MakeJoints forms a bond (a joint) between bricks if they have the required properties to.  If it is two smooth bricks, of course they won't joint together, but if one of them has a weld, then they will.
 
Brick Form:
<pre>
brick = game.Workspace.Brick
brick:MakeJoints()
</pre>
 
Model Form:
<pre>
model = game.Workspace.Model
model:MakeJoints()
</pre>
 
":MakeJoints()" is needed when making morphs, unless you want to take the long way out and use CFrame, which is not recommended. So as said before, it basically joints objects together.  When done to a model, everything is jointed to everything it can, simple as that.  On to the next subject, something similar to ":MakeJoints()".
 
===The ":BreakJoints()" Function===
This function breaks all joints already made. So if you have a building and its unanchored, you can always try breaking its joints and watch it crumble! This also works for a brick itself, it breaks any joints formed and becomes easily removable.
<pre>
--Brick Form
brick = game.Workspace.Brick
brick:BreakJoints()
 
--Model Form
model = game.Workspace.Model
model:BreakJoints()
</pre>
This is very simple. There isn't much more to be said except that if you do this, don't be shocked if your building falls apart.
 
===The ":GetMass()" Function===
So, ever wondered how heavy a brick is in ROBLOX? Well, you have two options. Either multiply the x by the y by the z, OR you can use ":GetMass()"! ":GetMass()" uses the simple equation of: X * Y * Z = Mass.  NOTE: You cannot edit mass.
<pre>
brick = game.Workspace.Brick
print(brick:GetMass())
</pre>
So we note out brick... Now, for the function line. We note our brick and we use the function. Now we print out the answer into the output, so let's say the brick was, in size, 10 by 2 by 5. What would it print out?  100.
 
===The ":MoveTo()" Function===
 
The ":MoveTo()" Function has two different types.  Both do different things, but have the same objective.  The first one will warp whole <b>models</b> to a specific destination.  We note our model, as always.  Then, on to our function line. We now use the ":MoveTo()" function to move the model to the position, 0,0,10:
<pre>
model = game.Workspace.Model
model:MoveTo(Vector3.new(0,0,10))
</pre>
 
The second ":MoveTo()" function is for the ''Humanoid''. This makes your character "run" to the position you noted.  In this script, we have two variables. The first notes the Humanoid and the second notes the base.  We run the function ":MoveTo()" and go to the position 0,0,0.  Now we seperate it with a comma and put the object we are standing on.  Your character is running by itself to the position you noted.
<pre>
me = game.Workspace.username.Humanoid -- change username
base = game.Workspace.Brick -- change Brick to the name of your base brick (Base by default)
me:MoveTo(Vector3.new(0,0,0), base) -- change 0,0,0 to where you want to go
</pre>
 
===The ":GetPlayerFromCharacter()" Function===
Ever needed to find out what player(not character) touched your brick in you button script? The ":GetPlayerFromCharacter()" Function makes it easier than actually searching the players with the previous ":FindFirstChild()" and ":GetChildren()" functions. What it does is automatically search an area for a the player that represents the character. If an Instance's "Controller" is "Player", you can use this function.
<pre>
DB=false
function onTouched(hit)
if DB==true then return end --if debounce is active then end the function
DB=true
if hit.Parent:FindFirstChild("Humanoid")~= nil then --If touched by a player then
if game.Players:GetPlayerFromCharacter(hit.Parent)~= nil then --If there is a player that goes with the character then
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
player.LocalStats.Cash=player.LocalStats.Cash + 5 --add 5 more cash
end
end
wait(1)
DB=false
end
 
script.Parent.Touched:connect(onTouched)
</pre>
 
That script will change give 5 cash to the local stat of cash if you touch it.
 
===The "lookVector" Function===
There actually is a CFrame function to find which way you are looking.  What this function does is find exactly which way your front is facing.  The "lookVector" function can be used for several different situations, e.g., tools, planes, robots, cars, etc.
 
Example:
<pre>
while true do
wait()
--[[ print(game.Workspace.Player.Torso.Velocity) If you want to print the velocity of your player as you go
along, use this.  Change "Player" to your username. --]]
 
--[[ print(game.Workspace.Player.Torso.CFrame.lookVector) If you want to see the lookvector of your player
printed out as you go along, use this.  Change "Player" to your username. --]]
 
game.Workspace.Player.Torso.Velocity = game.Workspace.Player.Torso.CFrame.lookVector * 60 --[[Change "Player"
to your username --]]
 
end
</pre>
 
First is 'while true do' loop.  Then is the 'wait()' function.  The script finds 'game.Workspace.Player,' (or game.Workspace.(yourusername), then it finds my Torso and sets its velocity to the way my Torso is looking by sixty.  This means this character will go at the speed of 60 units per second by the way it is facing.
 
== See Also ==
 
[[Function Dump]]
 
[[Category:Reference Pages]]

Latest revision as of 17:42, 6 April 2012

Redirect to: