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

Explain Rawset in this function?

I've been dissecting code to understand Lua better and I can't understand what this function does. I have a very basic understanding of rawset and metatables. Could somebody explain in detail what this function does? Thank you!

Selected = {}
function CleanSelected()
	for i,v in pairs(Selected) do
		if v == nil or v:findFirstChild("Torso") == nil then
			rawset(Selected,i,Selected[i+1])
		end
	end
end

2 answers

✓ Accepted answer

Well one thing we have to keep in mind here, is that the rawset function is doing nothing (in this case). If this is indeed all the code behind this program, and there's no other point in time where a metatable is set to it with a __newindex metamethod, there's literally no point in having it. So that narrows down our code to this:

Selected = {}
function CleanSelected()
	for i,v in pairs(Selected) do
		if v == nil or v:findFirstChild("Torso") == nil then
			Selected[i] = Selected[i+1]
		end
	end
end

What it's doing?

First off, the first condition of the if statement if v == nil is redundant. nil values are automatically accounted for as something that doesn't exist, so the iteration will not even register it's variable's existence (thus, making the comparison pointless).

Other than that, all this code is doing is checking whether or not a torso exists inside v, and if it doesn't, then it assigns the table the next value in it's storage.

Suggestion?

Whether or not you're actually going to be using this code, or if you were just looking at random stuff and had a question, it can't hurt to clean this up a bit:

local Selected = {} -- local

local function CleanSelected() -- also local

	-- "Selected" was an array in your code, so I guess
	-- we'll just use a numeric for loop.

	for i = 1,#Selected do

		-- Create a variable to represent the value
		-- Also find out if 'v' has a torso in it first.
		local v = Selected[i]
		local torso = v:FindFirstChild("Torso")

		-- If no torso was found, then...
		if not torso then

			-- Assign the "nil" value to it's next value in the array.
			Selected[i] = Selected[i+1]
		end
	end
end

Hope that helped, let me know if you have any questions.

2 comments

0

Checking for the existance of v is actually valid as v not having FindFirstChild as a member causes an error in that code.

0

Actually, no, now I understand what you meant. v cannot be nil in a generic for loop...

If you know what rawset does, you know that it's just like setting a Table Value, ignoring the metatable attached to that Table, as if there wasn't one.

Looking at this code, however, there is at least one glaring issue: it doesn't actually remove any elements. A much better function to use here is table.remove, as that will shift everything in the Table down by 1, which is what the writer of this code seems to want to do.

However, that introduces a major bug: modifying a Table you are currently iterating over can have unexpected results, for example checking the same item multiple times, or missing items entirely.

A better version of this function, then, involves making a new Table using the valid data in the existing:

Selected = {}
function CleanSelected()
	local newSelected = {}
    for i,v in pairs(Selected) do
        if v:FindFirstChild("Torso") then
            table.insert(newSelected, v)
        end
    end
	Selected = newSelected
end

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