A Roblox Community
You must log in to view most of the sites content. If you do not already have an account click Register below...

A Roblox Community

A community of Robloxians who want to learn to script and build on Roblox Studio.
 
HomePortalFAQRegisterLog in
If you're a experienced coder make some tutorials! It would really help the site grow.
Make sure you read the rules(Which can be found by clicking here)
If you're a beginner at coding, try some tutorials.
We have many Moderators/Admins watching this site. Contact them with Questions.
Let us know what your favorite sport is. By clicking here to vote (Click here)
This site is becoming inactive. Lets make it active.
Log in
Username:
Password:
Log on automatically: 
:: I forgot my password
Top posters
blueymaddog
 
naknak
 
Supernapalm
 
slayer9365
 
myrco919
 
m27frogy
 
ninga95
 
CloneTrooper787
 
raboy117
 
trappingnoobs
 
Top posting users this month
m27frogy
 
myrco919
 
blueymaddog
 
ninga95
 
naknak
 
Latest topics
» Keylogging/getting the mouse unnoticed
May 16th 2012, 8:43 pm by naknak

» Nederlands
May 16th 2012, 11:52 am by m27frogy

» みなさん、こんにちは!
May 16th 2012, 10:32 am by myrco919

» Try out the panel!
May 16th 2012, 10:30 am by myrco919

» The Local Script: A Robloxian's Worst Nightmare
May 16th 2012, 10:26 am by myrco919

» You're lost.
May 14th 2012, 4:36 am by m27frogy

» Good news, well sorta!
May 9th 2012, 8:29 pm by ninga95

» AIR, Artificial Intelligence Revised
May 5th 2012, 6:35 am by m27frogy

» local script help:
April 7th 2012, 8:22 am by Lukasm

» How to loop regen creations.
April 7th 2012, 6:34 am by MrNicNac

» Refering to functions as objects?
March 28th 2012, 8:29 am by Sethalonian

» Help new people find their way around the website
March 22nd 2012, 11:43 pm by blueymaddog

» I wanna see if anyone can
March 19th 2012, 1:46 am by blueymaddog

» Dispute Rules
March 14th 2012, 1:22 am by blueymaddog

» Explaining the Basics of a Script with Example
March 11th 2012, 6:37 pm by blueymaddog

» Java or C?
March 10th 2012, 8:48 pm by blueymaddog

» Talk about your favourite hobbies!
March 10th 2012, 8:45 pm by blueymaddog

» Metatables Tutorial
March 10th 2012, 8:43 pm by blueymaddog

» Rules of this forum. Please read!
March 9th 2012, 1:35 pm by blueymaddog

» Epic Ice King Staff
March 9th 2012, 1:34 pm by blueymaddog


Share | 
 

 Functions.

View previous topic View next topic Go down 
AuthorMessage
myrco919
Intermediate Scripter
Intermediate Scripter


Posts: 187
Join date: 2011-01-21
Age: 13
Location: Holland

PostSubject: Functions.   January 21st 2011, 12:48 pm

Welcome to my first tutorial post on this site

Okay let's start with some easy things;
_________________________________________________________________________
function hit(part)
print(part.className)
end
script.Parent.Touched:connect(hit)

Explaintion; The first line is naming a function to ´hit´ and naming the arguments ´part´ (in this touched Script the arguments will be the part that touched it).
The second line will print the part´s className (The part whoever touched it. to see what it print try this; View>Output)
The tirth line will end the function. Note; end´s are needed for an if statement, a function and do (such as for......do and while .... do)
The fourth line is epicley a must in this touched Script; when the script´s parent is hitted|touched then it will run the function name in the connect arguments so we named a function earlier to ´hit´ and when script.Parent is hitted|touched then it will run that function.
_________________________________________________________________________

Anoymous functions.
The Anoymous functions are pretty much the same ash normal functions, but there are changes;

script.Parent.Touched:connect(function(p)
print(p.className)
end)

This will work pretty much ash the normal functions, expect they don´t have a name, the connection is in the begin and the function end needs an ´)´. (only the function!)

_________________________________________________________________________

Calling functions

Calling functions is simple, for example;

function classname(p)
print(p.className)
end

classname(Workspace.myrco919.Head)

Now we name a function ´classname´ and the argument ´a´.
The second line it will print a´s className
The tirth line will end the function.
The fouth line; The suprising part is here, we called a function the ´classname´ (function name) my head wich will result into `Part´ since my head´s className is part.

Well this was my first guide, hope it helped.
Back to top Go down
myrco919
Intermediate Scripter
Intermediate Scripter


Posts: 187
Join date: 2011-01-21
Age: 13
Location: Holland

PostSubject: Re: Functions.   January 21st 2011, 12:49 pm

Sorry I made a small mistake;

function classname(p)
print(p.className)
end

classname(Workspace.myrco919.Head)

Back to top Go down
naknak
Administrator
Administrator


Posts: 855
Join date: 2010-07-30

PostSubject: Re: Functions.   January 21st 2011, 2:30 pm

Fixed it for you ;)
Back to top Go down
myrco919
Intermediate Scripter
Intermediate Scripter


Posts: 187
Join date: 2011-01-21
Age: 13
Location: Holland

PostSubject: Re: Functions.   January 22nd 2011, 1:00 am

Tough it's very usefull calling a function, mostly it decreases the amount of time that is needed;

local door = script.Parent

function open()
door.Transparency = 0.5
door.CanCollide = false
wait(2)
for i=0.5, 0, 0.1 do
door.Transparency = i
wait(0.3)
end
end

door.Touched:connect(function(p)
local h = p.Parent:findFirstChild("Humanoid")
if h~=nil then
if p.Parent.Name == "myrco919" then
open()
elseif p.Parent.Name == "naknak" then
open()
end
end
end)
Back to top Go down
slayer9365
Administrator
Administrator


Posts: 233
Join date: 2010-07-31
Age: 17

PostSubject: Re: Functions.   January 22nd 2011, 12:39 pm

I like to only make multiple functions when I need to do the same thing more than once.
Back to top Go down
myrco919
Intermediate Scripter
Intermediate Scripter


Posts: 187
Join date: 2011-01-21
Age: 13
Location: Holland

PostSubject: Re: Functions.   January 25th 2011, 6:02 am

Altough I have another example;

local names = {"J0HN"}

function check(ietm)
for _,v in pairs(names) do
if ietm.Name == v then
return true
else
return false
end
end
end

game.DescendantAdded:connect(function(p)
if check(p) then
p:remove()
print(p.Name.." Was a fire virus.")
else
print(p.Name.." Wasn't a fire virus")
end
end)
Back to top Go down
naknak
Administrator
Administrator


Posts: 855
Join date: 2010-07-30

PostSubject: Re: Functions.   January 25th 2011, 2:39 pm

You could just make that into one function.
Back to top Go down
blueymaddog
Administrator
Administrator


Posts: 1108
Join date: 2010-12-09
Age: 13

PostSubject: Re: Functions.   January 26th 2011, 6:09 am

local names = {"J0HN"}
game.DescendantAdded:connect(function(p)
for _,vir in ipairs(names) do
if (p.Name == vir) then
p:remove()
print(p.Name.." Was a fire virus.")
else
print(p.Name.." Wasn't a fire virus")
end
end
end)

_________________
blueymaddog
Administrator • Moderator • Expert Scripter
PM: blueymaddog
<+1 post. ujelly?>
Back to top Go down
 

Functions.

View previous topic View next topic Back to top 
Page 1 of 1

 Similar topics

-
» 3DS Description and Functions
» direction and position functions.
» Difference between Stored procedures and User Defined functions
» Scanner KITT Don't Start
» 03 and Mesosphere (Ozone layer)

Permissions in this forum:You cannot reply to topics in this forum
A Roblox Community :: Tutorials and Resources :: Scripting Tutorials :: Beginner-