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

How exactly do you make methods for parts (like :Destroy())?

Asked by 6 years ago
Edited 6 years ago

I came up with an idea to make a ModuleScript that calls a method that finds the Humanoid of a player and kills it...

(BTW apologies for the lengthy question)

I've been trying to test this idea in a regular script that doesn't kill nor is a modulescript. Here's the script I've got.

function getData(self) -- function with "self"; a.k.a. le method that I am hoping to get to function
    if self.Parent == nil then -- if part has already been destroyed
        warn("Part has no parent") -- warn (I'm too lazy to use error() lol)
    end
    print("Getting data on part...") -- this is just for fun
    wait(1)
    print("BEEP BOOP")
    wait(0.3)
    print("The selected part's name is "..tostring(self.Name)) -- Print the Name of the part
    wait(0.4)
    print("The selected part's FullName is "..tostring(self:GetFullName())) -- get the full name
    wait(0.4)
    print("The part's BrickColor is "..tostring(self.BrickColor)) -- get le brickcolor
    wait(0.4)
    print("The part's RGB color is "..tostring(self.Color)) -- get RGB
    wait(0.4)
    print("The part's Reflectance is "..tostring(self.Reflectance))
    wait(0.4)
    print("The part's Transparency is "..tostring(self.Transparency))
    wait(0.4)
    print("And finally, the part's position is... *que drumroll*")
    wait(1)
    print("..."..tostring(self.Position).."!!!!!!!1") -- finally le position
end
wait(1) -- countdown
print("3")
wait(1)
print("2")
wait(1)
print("1")
wait(1)
game.Workspace.i_am_a_part:getData() -- attempt to call the function as a method on the workspace part

But I get from the output:

>> getData is not a valid member of Part

When what I expect is:

>>3
>>2
>>1
>>Getting data on part...
>>BEEP BOOP
>>The selected part's name is i_am_a_part
>>The selected part's Full Name is game.Workspace.i_am_a_part
>>The part's BrickColor is Medium stone grey
>>The part's RGB color is 163, 162, 165
>>The part's Reflectance is 0.7
>>The part's Transparency is 0.3
>>And finally, the part's position is... *que drumroll*
>>...-6, 0.5, 4!!!!!!!1

However... If I run this in the same script instead:

game.Workspace.i_am_a_part:Destroy() -- destroy the part
wait()
local partCheck = game.Workspace:FindFirstChild("i_am_a_part")
if partCheck then
    print("Part not destroyed")
else
    print("Part destroyed.")
end

I immediately get the output: Part destroyed.

------------CONTINUED-------------

I've kept this question in my browser un-posted for a long time, hoping I could make :getData() function without posting this, but so far I have not succeeded in doing so, so I guess I'll just post it...

Here is the script at the point I posted this:

local Game = {} -- table to imitate "game"
local mt = {
    __index = function(self, i) -- make sure Instances exist in the table when called
        self[i] = i -- Attempt to set the value of the object to the Instance
        print(self[i]) -- meant to print the name of the instance
        print(self[i].ClassName) -- meant to print the ClassName of the instance
        -- iT = i.."ch" (These two lines were me thinking of spreading the metatable's code to the table... rejected it though. Wasn't gonna work anyways)
        -- iT = {}
        return self[i] -- return the instance
    end
}
setmetatable(Game, mt) -- set the metatable of Game to mt
function getData(self) -- ye olde method attempt
    if self.Parent == nil then
        warn("Part has no parent")
    end
    print("Getting data on part...")
    wait(1)
    print("BEEP BOOP")
    wait(0.3)
    print("The selected part's name is "..tostring(self.Name))
    wait(0.4)
    print("The selected part's FullName is "..tostring(self:GetFullName()))
    wait(0.4)
    print("The part's BrickColor is "..tostring(self.BrickColor))
    wait(0.4)
    print("The part's RGB color is "..tostring(self.Color))
    wait(0.4)
    print("The part's Reflectance is "..tostring(self.Reflectance))
    wait(0.4)
    print("The part's Transparency is "..tostring(self.Transparency))
    wait(0.4)
    print("And finally, the part's position is... *que drumroll")
    wait(1)
    print("..."..tostring(self.Position).."!!!!!!!1")
end
wait(1)
print("3")
wait(1)
print("2")
wait(1)
print("1")
wait(1)
Game.Workspace.i_am_a_part:getData() -- will it work? o wait, i'm cursed, it'll never work

In this current state, when run, the output prints Workspace when the last line runs - we want that. But then after that, it prints nil. I presume this means that it's calling the table object... but not the instance? Like, weird. Also, then it errors Workspace.Script:44: attempt to index field 'i_am_a_part' (a nil value). I do see why though.

This is the part that confuzzles me though: If I change self[i] = i to self[i] = game.i, it simply errors >>i is not a valid member of DataModel. Soo... I guess that's a good stopping point.

(Note: This is my first question, so I apologise for any inconveniences.)

RESPONSE TO LONG-AGO FIRST ANSWER: I don't see why I can't just finish the script and keep going on and implement it as a method (pun simultaneously intended and unintended) to kill players easily; I've already made progress that clearly can be progressed on. Also, I barely even can understand the code given (to clarify, the best game I've created is basically just an average baseplate, poor decoration, smolboi-level scripting, and several free models (generally of guests)). I mean... <cough cough cough> i just wanna... <cough> finish <cough> my <cough> script... <cough cough cough>

2:52 - Script, line 98: sweetkid01 has died

0
Ik, but methods normally only work with tables, that's my issue here sweetkid01 176 — 6y
0
once you destroy or remove the part, you can't gather information from it or make any changes (it will always return nil). One thing you can do is clone the part before destroying it. The clone will remain nil until you set it's parent. hellmatic 1523 — 6y

3 answers

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

game.Workspace.i_am_a_part:getData() If this is what you actually want then go with Kiriot22's answer. Which is very interesting.

Going the ModuleScript route you would do it this way -

Server Script:

local ServerScriptService = game:GetService("ServerScriptService")
local moduleScript = require(ServerScriptService:WaitForChild("ModuleScript"))
local part = workspace.Part

moduleScript.GetData(part)

Module Script:

local module = {}

function module.GetData(object)
    wait(1) -- countdown
    print("3")
    wait(1)
    print("2")
    wait(1)
    print("1")
    wait(1)

    if object and object.Parent then -- If Part exists and has a parent
        print("Getting data on part...") 
        wait(1)
        print("BEEP BOOP")
        wait(0.3)
        print("The selected part's name is "..tostring(object.Name)) -- Print the Name of the part
        wait(0.4)
        print("The selected part's FullName is "..tostring(object:GetFullName())) -- get the full name
        wait(0.4)
        print("The part's BrickColor is "..tostring(object.BrickColor)) -- get le brickcolor
        wait(0.4)
        print("The part's RGB color is "..tostring(object.Color)) -- get RGB
        wait(0.4)
        print("The part's Reflectance is "..tostring(object.Reflectance))
        wait(0.4)
        print("The part's Transparency is "..tostring(object.Transparency))
        wait(0.4)
        print("And finally, the part's position is... *que drumroll*")
        wait(1)
        print("... "..tostring(object.Position).." !!!!!!!1") -- finally le position
    else
        if not object then
            warn("Part doesn't exist") 
        else
            warn("Part has no parent")
        end
    end
end

return module

Outputs:

3
2
1
Getting data on part...
BEEP BOOP
The selected part's name is Part
The selected part's FullName is Workspace.Part
The part's BrickColor is Medium stone grey
The part's RGB color is 0.639216, 0.635294, 0.647059
The part's Reflectance is 0
The part's Transparency is 0
And finally, the part's position is... *que drumroll*
... -6, 0.496138126, -11 !!!!!!!1
0
welp, no choice I presume... :(( sweetkid01 176 — 6y
Ad
Log in to vote
0
Answered by
Amiaa16 3227 Moderation Voter Community Moderator
6 years ago

You can't create a custom function for roblox instance. You can however make a so-called "proxy" also known as object wrapping or sandboxing.

Basically, you make a fake userdata and set its metamethods to make it do the same things as the real instance, with the exception that you add the stuff you want.

Here's a simple wrapper example:

function Sandbox(obj)
    local fakeobj = newproxy(true)
    local meta = getmetatable(fakeobj)
    meta.__metatable = getmetatable(game)
    local customs = {
        getData = function(self)
            --do stuff
        end,
    }
    meta.__index = function(s,n)
        if customs[n] then
            return customs[n]
        else
            local s,e = pcall(function()
                return obj[n]
            end)
            if not s and e then
                return error("Sandbox error: " .. e)
            end
            if type(obj[n]) == "function" then
                return function(s, ...)
                    return obj[n](obj, ...)
                end
            else
                return obj[n]
            end
        end
    end
    meta.__newindex = function(s,i,v)
        obj[i] = v
    end
    meta.__tostring = function()
        return tostring(obj)
    end
    return fakeobj
end
0
:( sweetkid01 176 — 6y
0
But if I can get this close thou, y can't I just finish it up? sweetkid01 176 — 6y
0
Plus, this way I can't get it to work with killing players (my original intention), nor get it to function with a ModuleScript (i presume) sweetkid01 176 — 6y
0
Then instead of trying to do part:getData(), do getData(part) Amiaa16 3227 — 6y
View all comments (4 more)
0
Kiriot... I specifically want to make a method; not interested in regular functions :P sweetkid01 176 — 6y
0
i guess... no cookies for me :( sweetkid01 176 — 6y
0
You can use it in a module script. User#21908 42 — 6y
0
I can't believe nobody else has given any other answers... I just wanna finish the script, not revert to something else sweetkid01 176 — 6y
Log in to vote
0
Answered by
hellmatic 1523 Moderation Voter
6 years ago
Edited 6 years ago

Regarding to my comment:

local part = game.Workspace.i_am_a_part
local partclone = part:Clone()

part:Destroy() -- destroy the part

wait()

local partCheck = partclone
if partCheck then
    print("Part not destroyed")
else
    print("Part destroyed.")
end

0
you can gather information from the partclone (color, reflectance. pretty much all of it's properties lol) hellmatic 1523 — 6y
0
Err.... I never said I ran them at the same time lol sweetkid01 176 — 6y

Answer this question