Top posting users this month | |
Latest topics | » Where to go from here.September 14th 2020, 1:20 pm by MrNicNac» Send me an EmailSeptember 14th 2020, 1:16 pm by MrNicNac» [v1.6.0.0] Lua Script Obfuscator [No Bytecode]July 6th 2015, 7:38 pm by m27frogy» New Site PossiblyJuly 6th 2015, 4:16 pm by m27frogy» Ambassador!April 15th 2015, 11:40 pm by naknak» Boop - TagApril 13th 2015, 9:46 pm by naknak» Vip Class ScriptApril 13th 2015, 4:54 pm by naknak» Who's active?!April 13th 2015, 4:52 pm by naknak» Genesis PointJuly 17th 2014, 7:04 pm by branefreez» Reward SystemJuly 17th 2014, 5:41 am by m27frogy» Script RequestJuly 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 hereApril 26th 2014, 4:01 pm by altshiftkey» What's your favorite sport?January 1st 2014, 2:13 pm by m27frogy» FlashLight ScriptJanuary 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 CodingOctober 26th 2013, 1:12 pm by pook03» Simple ButtonSeptember 1st 2013, 6:19 pm by branefreez |
|
| Functions. | |
| | Author | Message |
---|
myrco919 Intermediate Scripter
Posts : 190 Join date : 2011-01-21 Age : 26 Location : Holland
| Subject: 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. | |
| | | myrco919 Intermediate Scripter
Posts : 190 Join date : 2011-01-21 Age : 26 Location : Holland
| Subject: 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)
| |
| | | naknak Administrator
Posts : 878 Join date : 2010-07-30
| Subject: Re: Functions. January 21st 2011, 6:30 pm | |
| | |
| | | myrco919 Intermediate Scripter
Posts : 190 Join date : 2011-01-21 Age : 26 Location : Holland
| Subject: 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)
| |
| | | slayer9365 Expert Scripter
Posts : 233 Join date : 2010-07-31 Age : 29
| Subject: 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. | |
| | | myrco919 Intermediate Scripter
Posts : 190 Join date : 2011-01-21 Age : 26 Location : Holland
| Subject: 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) | |
| | | naknak Administrator
Posts : 878 Join date : 2010-07-30
| Subject: Re: Functions. January 25th 2011, 6:39 pm | |
| You could just make that into one function. | |
| | | blueymaddog Administrator
Posts : 1081 Join date : 2010-12-09 Age : 26
| Subject: 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) | |
| | | Sponsored content
| Subject: Re: Functions. | |
| |
| | | | Functions. | |
|
| Permissions in this forum: | You cannot reply to topics in this forum
| |
| |
| Who is online? | In total there are 2 users online :: 0 Registered, 0 Hidden and 2 Guests
None
Most users ever online was 136 on October 21st 2024, 12:16 pm
|
| Please tell us what time zone you are in here
Got a really good idea? Post it in projects and it might show up here! |
|