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

Object Oriented Programming Random Methods?

I have made a few methods they work fine. How could I make it to where I want to call a random method without having to put all the code into one method. For example if I put the methods into an array they will all be called on the character and that not what I want. I could however put the code into a method called random but that seems like a lot of work.

**Method: **

function character:size(amount)
	local humanoid = self.location:findFirstChild("Humanoid") 
	if humanoid:findFirstChild("BodyHeightScale") then
		humanoid.BodyHeightScale.Value = amount
	end
	if humanoid:findFirstChild("BodyWidthScale") then
		humanoid.BodyWidthScale.Value = amount
	end
	if humanoid:findFirstChild("BodyDepthScale") then
		humanoid.BodyDepthScale.Value = amount
	end
	if humanoid:findFirstChild("HeadScale") then
		humanoid.HeadScale.Value = amount 
	end
end

3 comments

0

Include the code you are using, not discord attachments, not everyone has discord

1

There is only one method here. Can you explain a little more precisely what it is that you want to do?

0

So in the script I have I have multiple on for sizing one for color and one for making him take damage. I want to make it where it it chooses one of the three method randomly.

2 answers

✓ Accepted answer

"For example if I put the methods into an array they will all be called on the character" That's up to you, ex you could do local methodToRun = methods[math.random(1, #methods)]

(Edit)

Based on what you're trying to do, using anonymous methods may be better:

local methods = {
	function(self) self:color(0) end,
	function(self) self:size(10) end,
	--etc
}
-- to use a random one:
methods[math.random(1, #methods)](testCharacter) -- replacing testCharacter with whatever character you want to act on. Whatever you send will be sent as 'self' in the anonymous methods, which then call the function. ex, if the first method is randomly chosen, this effectively runs "testCharacter:color(0)"

2 comments

0

This is how I put them in the array local methods = {testCharacter:color(0),testCharacter:size(10)} and when the code read it they all started to take effect on the Dummy I have in the game

0

Thank you so much

So i read the comment you posted. You said you had the methods like so:

local methods = { testCharacter:color(0), testCharacter:size(10)}

But you are invoking the method right then and there. What you would want to do is reference the function . like so:

local methods = { testCharacter.color), testCharacter.size}

Then when you want to call it, just loop through the list or randomly choose one by using chess123mate answer and just add () to the end of methodToRun to run the method.

3 comments

0

I have tried this and got an error of ServerScriptService.character:78: attempt to index local 'self' (a nil value) also I wrote it like for i,v in pairs(methods) do v() end

0

You need to have module.__index = module in there so self can look for the methods

1

You also need to pass self as an argument when you call the function because when you do : it's automatically passed, but when you do . it has to be passed as an argument.

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