Common functions: Difference between revisions

From Legacy Roblox Wiki
Jump to navigationJump to search
>Emess
>Legend26
Tabbed code for readability
Line 11: Line 11:
print("Hi!")
print("Hi!")
delay(3, function()
delay(3, function()
print("Bye!")
print("Bye!")
end)
end)
</pre>
</pre>
Line 48: Line 48:


function onTouched(part)
function onTouched(part)
local h = part.Parent:FindFirstChild("Humanoid")
local h = part.Parent:FindFirstChild("Humanoid")
if h~=nil then
if h~=nil then
print("Hi There!")
print("Hi There!")
end
end
end
end


Line 68: Line 68:
local c = game.Workspace:GetChildren()
local c = game.Workspace:GetChildren()
for i=1, #c do
for i=1, #c do
if c[i].className == "Part" then
if c[i].className == "Part" then
bricks = bricks + 1
bricks = bricks + 1
end
end
end
end
print(bricks)
print(bricks)
Line 134: Line 134:
model = game.Workspace.Model
model = game.Workspace.Model
for _, v in pairs(model:GetChildren()) do
for _, v in pairs(model:GetChildren()) do
if (v:IsA("BasePart")) then
if (v:IsA("BasePart")) then
v:BreakJoints()
v:BreakJoints()
end
end
end
end
</pre>
</pre>
Line 195: Line 195:
DB=false
DB=false
function onTouched(hit)
function onTouched(hit)
if DB==true then return end --if debounce is active then end the function
if DB==true then return end --if debounce is active then end the function
DB=true
DB=true
if hit.Parent:FindFirstChild("Humanoid")~= nil then --If touched by a player then
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
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)
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
player.LocalStats.Cash=player.LocalStats.Cash + 5 --add 5 more cash
player.LocalStats.Cash=player.LocalStats.Cash + 5 --add 5 more cash
end
end
end
end
wait(1)
wait(1)
DB=false
DB=false
end
end


Line 219: Line 219:
<pre>
<pre>
while true do
while true do
wait()
wait()
--[[ print(game.Workspace.Player.Torso.Velocity) If you want to print the velocity of your player as you go
--[[ 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. --]]
  along, use this.  Change "Player" to your username. --]]
Line 226: Line 226:
printed out as you go along, use this.  Change "Player" to your username. --]]
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"  
game.Workspace.Player.Torso.Velocity = game.Workspace.Player.Torso.CFrame.lookVector * 60 --[[Change "Player"  
to your username --]]
to your username --]]



Revision as of 02:16, 9 July 2011

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 "delay()" Function

The delay function waits a certain amount of seconds and then executes a function on a new thread.

Example
print("Hi!")
delay(3, function()
	print("Bye!")
end)


The "wait()" Function

The wait function will delay, or wait, by seconds. Unlike other functions, this one can both be with or without a ":".

Example
print("Hi!")
wait(3)
print("Bye!")


In the output, this prints the word "Hi!", waits three seconds, then prints the word "Bye!".

See this page 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.

Example
game.Workspace.Brick:Clone().Parent = game.Workspace

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
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)


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. It is not really needed unless iterating through all the objects inside of an object.

Example
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)


The ":Remove()" Function

All the Remove function does is change its parent status to nil. A possible alternative is "object.Parent = nil" instead. But using this method is typically 'better', due to the object orientated programming rule that if you can use a method to change a variable, use it instead. Example:

Example
game.Workspace.Brick:Remove()


The ":MakeJoints()" Function

MakeJoints forms a bond (a joint) between bricks if they have the required surfaces types:

SurfaceType Weld Universal Studs Inlet Glue Smooth
Weld Weld Weld Weld Weld Weld Weld
Universal Weld Snap Snap Snap Glue No Joint
Studs Weld Snap No Joint Snap Glue No Joint
Inlet Weld Snap Snap No Joint Glue No Joint
Glue Weld Glue Glue Glue Glue Glue
Smooth Weld No Joint No Joint No Joint Glue No Joint
Example

Brick Form:

brick = game.Workspace.Brick
brick:MakeJoints()

Model Form:

model = game.Workspace.Model
model:MakeJoints()


The ":BreakJoints()" Function

This function breaks all joints already made. Running this on a building all grouped into a model might cause it to crumble, although bricks stacked on each other will be unaffected until another force acts on them.

Example
--Brick Form
brick = game.Workspace.Brick
brick:BreakJoints()

--Model Form
model = game.Workspace.Model
model:BreakJoints()

The model form is the same as doing so:

model = game.Workspace.Model
for _, v in pairs(model:GetChildren()) do
	if (v:IsA("BasePart")) then
		v:BreakJoints()
	end
end


Although the model form is much simpler.

The ":GetMass()" Function

So, ever wondered how heavy a brick is in ROBLOX? The GetMass() function is just for that! It is the same as multiplying all the components of the size of the brick together. NOTE: Editing mass is not possible, as it is a returned value from a function.

Example
brick = game.Workspace.Brick
print(brick:GetMass())

This is the same as...

brick = game.Workspace.Brick
print(brick.Size.X * brick.Size.Y * brick.Size.Z)


If density existed in ROBLOX, GetMass() would multiply the size components and the density. For the time being, all parts in ROBLOX have a density of 1.

The ":MoveTo()" Function

The MoveTo() function has two different uses. The first is usable on a Model or any type of Part (see BasePart.)

Example
--Used on a model...
model = game.Workspace.Model
model:MoveTo(Vector3.new(0, 0, 10))

--Used on a Part...
brick = game.Workspace.Brick
brick:MoveTo(Vector3.new(0, 0, 10))


The second use of MoveTo() is to force a Humanoid to walk to a location to the best of its ability. The Humanoid will continue to walk in that direction until it reaches the destination.

This version of MoveTo() requires three arguments, the first is given by the : operator, and is the Humanoid. The second is the target location, and finally, the third is the BasePart the Humanoid is walking on to get to the destination.

Example
Character = game.Workspace.SomePlayer.Humanoid
Base = game.Workspace.Base --Assuming your baseplate is named "Base"
Character:MoveTo(Vector3.new(15, 0, 10), Base) --Tell Character to talk to (15, 0, 10) on Base.


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.

Example
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)


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


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