Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Object Oriented Programming Random Methods?

Asked by 5 years ago
Edited 5 years ago

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

0
Include the code you are using, not discord attachments, not everyone has discord programmerHere 371 — 5y
1
There is only one method here. Can you explain a little more precisely what it is that you want to do? BlueTaslem 18071 — 5y
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. BreadDestroyer 22 — 5y

2 answers

Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

"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)"
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 BreadDestroyer 22 — 5y
0
Thank you so much BreadDestroyer 22 — 5y
Ad
Log in to vote
1
Answered by 5 years ago

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.

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 BreadDestroyer 22 — 5y
0
You need to have `module.__index = module` in there so self can look for the methods addictedroblox1414 166 — 5y
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. addictedroblox1414 166 — 5y

Answer this question