A Roblox Community

A community of Robloxians who want to learn to script and build on Roblox Studio.
 
HomeFAQRegisterLog 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 in automatically: 
:: I forgot my password
Top posters
blueymaddog
 
naknak
 
Supernapalm
 
m27frogy
 
slayer9365
 
myrco919
 
branefreez
 
ninga95
 
CloneTrooper787
 
raboy117
 
Top posting users this month
Latest topics
» Send me an Email
January 8th 2016, 10:14 am by naknak

» [v1.6.0.0] Lua Script Obfuscator [No Bytecode]
July 6th 2015, 7:38 pm by m27frogy

» New Site Possibly
July 6th 2015, 4:16 pm by m27frogy

» Ambassador!
April 15th 2015, 11:40 pm by naknak

» Boop - Tag
April 13th 2015, 9:46 pm by naknak

» Vip Class Script
April 13th 2015, 4:54 pm by naknak

» Who's active?!
April 13th 2015, 4:52 pm by naknak

» Where to go from here.
April 13th 2015, 4:51 pm by naknak

» Genesis Point
July 17th 2014, 7:04 pm by branefreez

» Reward System
July 17th 2014, 5:41 am by m27frogy

» Script Request
July 10th 2014, 11:43 am by naknak

» local scripts?
July 10th 2014, 11:39 am by naknak

» Project: Reconstruction [Died]
July 10th 2014, 11:36 am by naknak

» Hi. I am new here
April 26th 2014, 4:01 pm by altshiftkey

» What's your favorite sport?
January 1st 2014, 2:13 pm by m27frogy

» FlashLight Script
January 1st 2014, 2:11 pm by m27frogy

» Gun Making! [READ DESC]
January 1st 2014, 2:10 pm by m27frogy

» Hi, I am new here!
November 26th 2013, 3:33 pm by Keanu73

» Improve Coding
October 26th 2013, 1:12 pm by pook03

» Simple Button
September 1st 2013, 6:19 pm by branefreez


Share | 
 

 Functions.

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

Posts : 190
Join date : 2011-01-21
Age : 19
Location : Holland

PostSubject: Functions.   January 21st 2011, 4: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
avatar

Posts : 190
Join date : 2011-01-21
Age : 19
Location : Holland

PostSubject: Re: Functions.   January 21st 2011, 4: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
avatar

Posts : 878
Join date : 2010-07-30

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

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

Posts : 190
Join date : 2011-01-21
Age : 19
Location : Holland

PostSubject: Re: Functions.   January 22nd 2011, 5: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
Expert Scripter
Expert Scripter
avatar

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

PostSubject: Re: Functions.   January 22nd 2011, 4: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
avatar

Posts : 190
Join date : 2011-01-21
Age : 19
Location : Holland

PostSubject: Re: Functions.   January 25th 2011, 10: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
avatar

Posts : 878
Join date : 2010-07-30

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

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

Posts : 1081
Join date : 2010-12-09
Age : 19

PostSubject: Re: Functions.   January 26th 2011, 10: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
Sponsored content




PostSubject: Re: Functions.   

Back to top Go down
 
Functions.
View previous topic View next topic Back to top 
Page 1 of 1

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