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

How do I reference a method?

Lets say I want to ban an object's method from being used. This requires me to know what method needs to be banned, hence the question.

This isn't a roblox Default object, like Part, but rather a theoretical object held together by a table.

In order to ban a method from it, I'm going to give the object a ban method that takes in the argument of a method and adds it to the ban list. That way it only needs to glimpse at the ban list to see if the method is within it to determine what function can run.

local Object =  {}
function Object.new(someName)
	name = someName;
	bannedList = {}
	temp = {}

	function temp:Print()
		if checkValidity(temp:Print()) then  --This won't work, will cause a stack overflow.
			print(name);
		end
	end
	function temp:Ban(someMethod)
		table.insert(bannedList, someMethod = true) --not quite sure if this is the right dictionary-key reference here
	end
	function checkValidity(someMethod)
		if bannedList[someMethod] ~= nil --if it's there
			return false --invalid
		else
			return true -- valid
		end
	end
	return temp;
end

return Object

--Driver
test1 = Object.new("Bob");
test1:Print();
test1:Ban(test1:Print()); -- Again, will not work
test1:Print(); --Should do nothing

As you can see, I'm trying to make it that some Instance can tell Object not to use its methods. Though I don't know how to refer to them (Is it something like Object:Ban?)

How do I do this reference? Kudos to whoever could make a more efficient method to ban a function as well

1 answer

A method is just a member function which is invoked with the first argument implicitly as the container. Your system is actually a little flawed as it's not being properly applied, but nonetheless I will show you how it can do it.

function temp:Print()
        if checkValidity(temp.Print) then 
            print(name);
        end
    end
    function temp:Ban(someMethod)
        bannedList[someMethod]  = true;
    end

At this point, you should notice that your ban function now requires a function and not a name. This is your fault, and I blame you. I'm not going to fix it, because I'm lazy and it's not my problem.

I hope I helped

3 comments

0

"This is your fault, and I blame you. I'm not going to fix it, because I'm lazy and it's not my problem." Downvoted because of that. Do not use put-downs on a user.

0

I'm not trying to put them down, I'm trying to tell them that any problems they come across with technicalities are their fault, and it's not my job nor anyone else's to fix them. I'm sorry I upset you enough to make you downvote a reasonably correct answer.

0

Okay my insane misconception of table.insert taking arguments that aren't integer values is fixed, but the whole problem was using the function instead of a name in the first place. I just wanted to see how possible this was - I believe I've found another solution to my actual problem I have.

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