Lua questions #2

Im making multiple post asking questions so that any questions don’t get cluttered.

Please also check lua questions 1


What does pcall() do, and why is it used in datastores, when should I use it?

How does the Datastore Service Work?

What is a tuple?

Whats the difference from Arguments and Parameters?

What is invoking in luau?

What does setfenv do, when should I use it?

What is type, how does it work, when should I use it?

What does pack and unpack do? When should I use them?

What does xpcall do, whats the difference from pcall and when should it be used?

What is _G When do I use it?

What is _VERSION When should I use it?

Do you know how to google these questions?

2 Likes

For me, its easier to ask people on the Devforum, especially because everything online ( usually more than 100,000 results ) don’t help with clarification. And a lot of source’s aren’t that reliable that i’ve seen.

So I ask questions like these here instead.

1 Like

Hello!

Pcall functions basically catches if any errors happen in the function. The reason why they should be used inside of things when using data stores is because the API that datastores use can sometimes go down which can cause errors in your script causing the whole code to stop working as it should.

If you ever see this message in the docs (old ones) then you should use a pcall function:

In the new docs it looks like something like this:

What do you mean by “How does the Datastore Service Work?”. What part do you need to understand? In general you just get the datastore service and then get the data store from the service and then once you have the data store do things like getAsync and as such.

Inside of Lua a tuple is a list of variables. Often you will find them inside of things such as a method accepting multiple variables/arguments.

You can get a bit more info here: Tuple.

To be honest I am not really sure how to explain what they are but here is an image I found on google which shows it well (language in the image is not lua but you get the idea).

image

Invoking (or at least what I know it off) is normally where you are expecting something back. An example of invoking in a remote function where you invoke the server or client and it expects back a value.

Now I have to admit I was not 100% sure about what setfenv and getfenv does. After some quick research it seems to allow you to set a script global variables (also known as environmental/env variables). After doing some research I kinda understand a bit (I kinda get it from using .env in node.js for Discord bots and websites) but still don’t massively understand the use of it (even in the post I will link below which explains it very well said you are very unlikely to ever use it).

Basically allow you to get the datatype of a Roblox datatype.

I highly recommend checking this post. It explains xpcalls, pcalls and ypcalls very well.

All it does is gives you the current version of Lua. It’s really not at all important for game devs and see 0 uses of it tbh.

1 Like

Alright, ill tell you what I know.

Pcall

Used to call a function that might return an error, returns 2 parameters, the first is if it was a success or the function errored, the second is the value which was return from that function

Why is it used in Datastores

DatastoreService could sometimes return errors for reaching a limit of Datastores thats why we use Pcall

Tuples

Tuples are just array-like tables with a finite known number of indexes (which are often immutable its values can not be changed)

Tuples are not natively supported by Lua as they should be thought of as immuatable. This is not enforced at runtime.

This is a tuple with a length of 3

local tuple = {1 , 2 , 3}

Parameters and Arguments

Parameters are what the function needs
Arguments are what you give for the function.

function and(a , b) --a and b are parameters

end

and("a" , "b") --here they are arguments

Type

lets say you are passing coins (number) through a remote Event and an exploiter fires a string, you want to check what type of item he passed, so use type(item), it returns a string containing the type of that item, just an extra check.

Pack and Unpack

Pack just puts all the given arguments to a table, you can use it to check how many were provided
Unpack are used to return everything inside a table, maybe you want to pass some letters a player need to write without using alot of parameters, send them as a table and unpack them

_G

_G is a global way of defining a variable, its better to use a ModuleScript tho

_VERSION

returns the version of lua, not very useful

Hope this helped!

1 Like

How does the API Work? ( Functions, Properties, etc… )

Is a method also referred to a function? And do methods have arguments, tuples, or parameters in them?

So like using .OnServerEvent or .OnClientEvent?

Why does the first one print out userdata, and what even is userdata?

is there a zpcall

Why are tuples important?

But what if the exploiter fires a numberValue? how would you make sure to not make that exploitable? Could you give a possible example?

Can you give me an instance when this is used?

Tuples are normally used when you want to make a table that you will NOT change, just a simple table, nothing different.

If he passed a number you can add other checks, it differs from each game to another, type will just check if its a number so the script doesnt error and break the game

I once wanted to make something happen when you click an UNKNOWN amount of keycodes so i used to send all keycodes as “…” And in the function i used to pack then and loop through them
I use unpack when i dont want alot of parameters so lets say you are passing coins, gems, pets, level, xp, etc you cant have all these as parameters to a function its just too much, so pass then as a table and unpack the table

local coins , gems , pets , level , xp = Unpack(table)

Another question (wasn’t for me)

So like using .OnServerEvent or .OnClientEvent?

Invoke is .OnServerInvoke and .OnClientInvoke
But you can return items from it, when using .OnServerEvent and .OnClientEvent you cant return values

You can think of types as an identifier for all objects in Roblox Studio (BasePart, RBXScriptConnection, thread, string, number).

Typechecking is used as a linting tool so that we can specifically tell Roblox Studio what something is. If you input something that isn’t the exact same as the specified type, it will warn you about it. However, this won’t error when you run the game.

You can use typechecking as a way to find out any potential errors more easily.

local stuff: boolean = true

stuff = 1 -- Type `number` could not be converted into `boolean`
local function Test(var: boolean)
	-- do something
end

Test("string") -- Type `string` could not be converted into `boolean`

You can also use this to make in-built variables or functions that are specific to that type to be shown in the autocomplete bar.

for _,v: PointLight in ipairs(workspace:GetDescendants()) do
	if v:IsA("PointLight") then
		v.Brightness = 5
	end
end

Now, for the keyword type. It can be used to create custom types.

type test = {number}

local array: test = {
	5, 6, 11, 15, "uh oh this isnt a number" -- Type `string` could not be converted into `number`
}

A more in-depth explanation for typechecking can be found here.

Just look at the documentation. All the services and everything are very well documentation.

That is normally for firing servers (events). Invoking use different things such as RemoteFunction.OnClientInvoke

Not that I know off.

The way would be to protect back end. There is not much you can really do and you should try your best not to transfer data exploiters could exploit and use against you.

I made a post on this: How to protect your server from exploiters tutorial!

1 Like