Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

I need help understanding key words and simple basic scripting stuff help?

I’m not understanding key words and some basic scripting stuff. I’ll put them all below. Please, if you can, answer what they mean and do and how they’re useful as well as how they can be used in games as an example. Thanks!

keyword1 = pairs
keyword2 = ipairs
keyword 3 = next
- - [[for loops (numeric and generic as well as _, and i,v and such also explain the syntax?)
return (why not print?)
debounces
parameters and arguments (why do some functions have hit as a parameter and a, b or some kind of input variables? Don’t they all have to be similar? Also why do people use func or ... as a parameter?) - -]]

7 comments

0

There is something called variable names and such...

0

Okay, so pairs loop through tables, ipairs wastes time checking unnecessary stuff, next is not useful, for loops can be found on youtube, return and print are two different things, debounces are like cooldowns, parameters are the info you send to functions, and arguments are the parameters you receive.

0

I’m just showing them what do you want me to do put them in variables?

0

Oh ok

0

Please read the wiki in the future.

0

@lukeb50 I have I don’t understand it however

1

I believe this question is a good example of what should be on this site -- people asking what things mean, rather than just "my script isn't working, please help" =)

2 answers

✓ Accepted answer

This answer is meant to add to H4X0MSYT's (who did a good job summarizing).

pairs is best used on a table that has both non-integer keys and values, whereas ipairs should only be used for tables that have integer keys (ie those that you can treat like an array). ex:

local myTable = {
	RandomNumber = 5,
	Greeting = "Hi",
	AnotherTable = {},
}
for k, v in pairs(myTable) do
	print(k, v)
end

local list = {5, "Hi", {}}
for i, v in ipairs(list) do
	print(i, v)
end

Note that pairs will get everything that ipairs does, but not the reverse. Also, ipairs is essentially the same as doing this:

local list = {5, "Hi", {}}
for i = 1, #list do
	print(i, list[i])
end

You really don't need to know about next when you're using pairs/ipairs.

You can read more information on for loops/their syntax on the lua manual.

return has two uses: return early (ex if a condition isn't met) and/or return a value. An example of a function returning a value would be tostring, which "return"s the desired string. As for returning early, an example: H4X0MSYT's debounce code can be written:

local debounce = false

function Main()
	if debounce then return end -- return early because we don't want to do anything if we're already doing something in Main
	debounce = true
	-- Code here
	debounce = false
end

A common case for using debounce is when you're listening to a Touch event and have a wait.

Parameters and Arguments:

function Example(a, b) -- 'a' and 'b' are parameters. You can think of 'a' as the name of the parameter.
	return a + b
end
local c = 5
print(Example(c, 2)) -- 'c' and '2' are arguments. ex, 'c' is being passed in for the parameter 'a'. Also, when Example is running, it's "b" parameter will have the number 2.

You can pass anything into a function, including other functions (used to add functionality to a function -- the Roblox wiki shows how to do this with debounce). The ... is called "varargs" and represents "any number of arguments". You can use it like this:

function Sum(...)
	local args = {...} -- capture the '...' into something usable
	local sum = 0
	for i = 1, #args do
		sum = sum + args[i]
	end
	return sum
end
print(Sum()) -- 0
print(Sum(1)) -- 1
print(Sum(1, 2, 3, 4)) -- 10
print(Sum(1, "Hello")) -- this will error when Sum attempts to add "Hello", but lua is okay with passing any type of value(s) into the vararg.

[Edit - Follow up to comment]

[]: This indexes a table -- that is, retrieves the "value" associated with a specific "key". ex:

local t = {} -- The {} creates a new table
t["SomeKey"] = "SomeValue" -- can also be written: t.SomeKey = "SomeValue"
--Once we've done that, we can retrieve the value by using the key:
print(t.SomeKey) -- will print out "SomeValue". Instead of printing it, we could assign it to a variable, pass it to a function, or whatever else we want to do with it.

--Note that you can use *anything* as a key or value except "nil". ex:
t[Vector3.new(1, 2, 3)] = 3

--It is illegal to use "nil" as a key. If you use "nil" as a value, you remove that key from the table. ex:
t.SomeKey = nil -- now 'pairs' won't show you this key anymore unless you re-add it to the table

--The point of pairs is to go over every key and value in the table, in any order.
--ipairs goes over every *integer* key (ex 1, 2, 3, etc) and value, in numeric order. Note that if you have a gap in the table, ipairs might not print it. ex:

local t = {"a", "b"}
t[4] = "d"
for i, v in ipairs(t) do
	print(i, v)
end
--[[The loop could print either of:
1 "a"
2 "b"

OR

1 "a"
2 "b"
4 "d"

You can't depend on it either way, so don't leave gaps in your tables if want to use ipairs.]]

Regarding your debounce question: if debounce then return end, to be clear, is just this:

if debounce then
	return
end
--rest of function here

You appear to be asking why the "return" is necessary? The "end" on line 3 only means "this spot marks the end of the if block", not the end of the function. An "end" doesn't mean "end what you're doing", just "the end of the most recently started block". ex:

function Something()
	if someCondition then
	elseif otherCondition then
	end -- this "end"s the 'if'. This must be done before you end the function.
end -- this "end"s the function.

So, without the return in the debounce code, lua would keep executing the rest of the function regardless of whether debounce was true or not.

Another thing I didn't mention before is that return can return as many values as you like:

function ReturnTwoThings()
	return 1, 2
end
local a, b = ReturnTwoThings()
print(a + b) -- 3

Do note that it's usually easiest if you don't return too many separate values - most common is to just return 0-1 values, though 2 isn't unusual either.

1 comment

0

Wow thank you so much this has helped a lot I will surely reference to this and use it for the future. Also what is [] and why is it used. Could you also explain why at if debounce then return end why we return and not just end and what the difference makes

pairs: Part of loops. Used for non tables. ipairs: Another part of loops. Used for tables. next: You guessed it, more loops! for loops: It's saying for everything this, run this. Example:

for i, v in pairs(game.Workspace:GetChildren()) do -- For everything in workspace,
	if v.Name == "Baseplate" then                          -- Run this code.
		print("WooHoo!")
		return
	end                                                                   --
end

return: halts the functon and returns the value. Example:

function ReturnParent(Arg1)
	return Arg1.Parent
end

Although that function is useless for finding a parent and just stupid. debounce: It's a way of making a script not run again while it's allready running. Example:

local Debounce = false

function Main()
	if Debounce == false then
		Debounce = true
		-- Code here
		Debounce = false
	end
end

Now we go to args and params. Lets say I to tostring() which converts a number to string. What does it convert? We give it an arg to tell it what to do. Also the things like hit, you haven't been looking at cracked games have you? a,b etc represent an input needed. IDE's specify things like (STRING text, BOOL retry, NUMBER retries) to tell us what kind of value we give it. If it's incorrect, it will throw an error. Buttttt as everyone else said, Read the wiki. EDIT: We use functions in args just to make life alot easier.

game.Players.PlayerAdded:connect(function(Plr)

end

will do the same thing as

function PlrAdder(Plr)

end
game.Players.PlayerAdded:connect(PlrAdded)

We also use functions in certain things like DataStore UpdateAsync(). We give it a function as an arg because it's easier than making a function somewhere else to do it.

5 comments

0

pairs is a iterator function ONLY for tables, not for non-tables. (how'd that work with non tables?)

0

What is arg1’s Parent

0

Also when i said func in arguments I literally meant the word “func”

0

thats just a name for a varible.

0

Lmao accepts other answer and not mine gg no re.

This discussion is preserved as it was. New answers, comments, and votes are closed.