The return statement can be used to feed data from a different operation or function.
For example, dictionaries are made using hashmaps so they cannot be measured like normal arrays.
1 | local myDict = { [ "John" ] = 17 , [ "Jane" ] = 18 } |
2 | local myTab = { "John" , "Jane" } |
In this case, to measure the length of the dictionary I'm going to use a function.
01 | function findLength(table) |
03 | for i, v in pairs (Table) do |
11 | print ( "My dictionary is " ..findLength(myTab).. " elements long." ) |
Below is a function that I have used in the past to determine the security level on a player's keycard shortly after spawning. I call assignClearance from within a function which writes on the side of a handheld tool.
01 | function assignClearance(player, groupId) |
02 | player:GetRankInGroup(groupId) |
04 | if player:IsInGroup(GroupID) and (player:GetRankInGroup(GroupID) = = 255 ) then |
06 | elseif player:IsInGroup(GroupID) and (player:GetRankInGroup(GroupID) > = 250 ) then |
08 | elseif player:IsInGroup(GroupID) and (player:GetRankInGroup(GroupID) > = 248 ) then |
10 | elseif player:IsInGroup(GroupID) and (player:GetRoleInGroup(GroupID) = = "Under Investigation" ) then |
12 | elseif player:IsInGroup(GroupID) and (player:GetRankInGroup(GroupID) = = 3 ) then |
14 | elseif player:IsInGroup(GroupID) and (player:GetRankInGroup(GroupID) = = 2 ) then |
16 | elseif player:IsInGroup(GroupID) and (player:GetRankInGroup(GroupID) = = 1 ) then |
In short, the return statement helps you keep your code more robust and reusable as it often saves you from having to repeat blocks of code over again.