generic image
Processing...
  • Games
  • Catalog
  • Develop
  • Robux
  • Search in Players
  • Search in Games
  • Search in Catalog
  • Search in Groups
  • Search in Library
  • Log In
  • Sign Up
  • Games
  • Catalog
  • Develop
  • Robux
   
ROBLOX Forum » Game Creation and Development » Scripting Helpers
Home Search
 

Re: Metatables

Previous Thread :: Next Thread 
notsopwnedg is not online. notsopwnedg
Joined: 07 Nov 2010
Total Posts: 4182
05 Feb 2014 06:17 PM
This is kind of hard to explain so.

Inside the connect method
How could I access the variable Bob that indirectly called the method?


-------------------------------------------------------------------------
local Player = {
Events = {
Spawned = {
connect = function (Func)
--Player.CharacterAdded:connect(Func)
end ;
};
Died = {};
};
}

setmetatable(Player,{
__index = function(s,k)
if s.Events[k] then
return s.Events[k]
end
end
})

setmetatable(Player.Spawned,{

})

function Player:New(Playery)
local Object = {}
setmetatable(Object,{
__index = function(s,k)
return Player[k] or error("Cannot invoke "..k.." on this object")
end
})
Object.Player = Playery
return Object
end

local Bob = Player:New("I am the player object")
Bob.Spawned:connect(function()
print("Player has spawned")
end)
Report Abuse
notsopwnedg is not online. notsopwnedg
Joined: 07 Nov 2010
Total Posts: 4182
05 Feb 2014 06:46 PM
...
Report Abuse
notsopwnedg is not online. notsopwnedg
Joined: 07 Nov 2010
Total Posts: 4182
05 Feb 2014 10:03 PM
...
Report Abuse
notsopwnedg is not online. notsopwnedg
Joined: 07 Nov 2010
Total Posts: 4182
11 Feb 2014 11:58 PM
.
Report Abuse
notsopwnedg is not online. notsopwnedg
Joined: 07 Nov 2010
Total Posts: 4182
11 Apr 2014 07:03 PM
Bump
Report Abuse
AgentFirefox is not online. AgentFirefox
Top 100 Poster
Joined: 20 Jun 2008
Total Posts: 22404
11 Apr 2014 07:04 PM
Where exactly do you want to use 'Bob' in your script?
Report Abuse
notsopwnedg is not online. notsopwnedg
Joined: 07 Nov 2010
Total Posts: 4182
11 Apr 2014 07:08 PM
Inside this function


connect = function (Func)
--Player.CharacterAdded:connect(Func)
end

I'm just trying to create a custom player object with custom events.....I guess i could just leave out the connect method making this a lot simpler...
Report Abuse
ForeverDev is not online. ForeverDev
Joined: 04 Oct 2008
Total Posts: 13300
11 Apr 2014 07:17 PM
This is Roblox, metatables aren't extremely important to be honest.

Can you put your script on pastebin and Lua format it? I can't look at a long script when it isn't formatted correctly :P
Report Abuse
cntkillme is not online. cntkillme
Joined: 07 Apr 2008
Total Posts: 44956
11 Apr 2014 07:18 PM
Why would you want to return "Bob" if you already have it though?
Report Abuse
notsopwnedg is not online. notsopwnedg
Joined: 07 Nov 2010
Total Posts: 4182
11 Apr 2014 07:19 PM
I know they are not really that useful(Here). I just want to mess around with them

pastebin.com/HPMPSnz2
Report Abuse
cntkillme is not online. cntkillme
Joined: 07 Apr 2008
Total Posts: 44956
11 Apr 2014 07:23 PM
I see future errors but none that are always going to occur (as far as I can see)

Fix 1: 'connect = function (x, Func)'
Fix 2: 'return rawget(Player, k) or error("Cannot invoke "..k.." on this object")'
Report Abuse
notsopwnedg is not online. notsopwnedg
Joined: 07 Nov 2010
Total Posts: 4182
11 Apr 2014 07:23 PM
I don't have "bob" inside of the connect function....
Report Abuse
notsopwnedg is not online. notsopwnedg
Joined: 07 Nov 2010
Total Posts: 4182
11 Apr 2014 07:24 PM
Yea....
Report Abuse
notsopwnedg is not online. notsopwnedg
Joined: 07 Nov 2010
Total Posts: 4182
11 Apr 2014 07:28 PM
Using rawget actually breaks the current script............
Report Abuse
cntkillme is not online. cntkillme
Joined: 07 Apr 2008
Total Posts: 44956
11 Apr 2014 07:31 PM
It should work fine, since your way is going to cause a stack overflow if the key doesn't exist.
And same with 'if s.Events[k] then'
Report Abuse
notsopwnedg is not online. notsopwnedg
Joined: 07 Nov 2010
Total Posts: 4182
11 Apr 2014 07:31 PM
Yes i understand that but...

18:28:22.349 - local Player = {
Events = {
Spawned = {
connect = fun:28: Cannot invoke Spawned on this object
18:28:22.351 - Script 'local Player = {
Events = {
Spawned = {
connect = fun', Line 28 - index Spawned
18:28:22.352 - Script 'local Player = {
Events = {
Spawned = {
connect = fun', Line 36
18:28:22.353 - stack end

What?
Report Abuse
cntkillme is not online. cntkillme
Joined: 07 Apr 2008
Total Posts: 44956
11 Apr 2014 07:34 PM
Oh pssht, no wonder. For a second I thought it was setting the metatable to Player, not Object. You don't need that part but you should fix the second thing I mentioned (So: setmetatable(Player,{
__index = function(s,k)
return rawget(s.Events, k)
end
}))
Report Abuse
AgentFirefox is not online. AgentFirefox
Top 100 Poster
Joined: 20 Jun 2008
Total Posts: 22404
11 Apr 2014 07:34 PM
I'd reformat the script to put everything in your new method, that way your 'Player' table has access to your Object table.

Also, the way your current format works, it completely defeats the purpose of a metatable on Object, even as a wrapper.

Basically, you want to make a single metatable if possible, and have any tables that would be duplicates (as in, unique to each player) to be contained inside the new method.

Would you like to see what I mean?
Report Abuse
notsopwnedg is not online. notsopwnedg
Joined: 07 Nov 2010
Total Posts: 4182
11 Apr 2014 07:39 PM
I mostly understand what you mean...I would like to see what you mean though.
Report Abuse
AgentFirefox is not online. AgentFirefox
Top 100 Poster
Joined: 20 Jun 2008
Total Posts: 22404
11 Apr 2014 07:42 PM
I will address the security issues with your script as well. Give me a bit of time to type up the thing; I haven't worked with metatables for a few weeks.
Report Abuse
notsopwnedg is not online. notsopwnedg
Joined: 07 Nov 2010
Total Posts: 4182
11 Apr 2014 07:47 PM
I guess I could just do something like this that does not use metatables...



local Player = {
New = function (_,Player)
local Object = {
Spawned = {
Connect = function(Func)
Player.CharacterAdded:connect(Func)
end
}
}
return Object
end
}

local Player = Player:New(Game.Players.Bob)
Player.Spawned:Connect(function()
---LALALALA
end)
Report Abuse
AgentFirefox is not online. AgentFirefox
Top 100 Poster
Joined: 20 Jun 2008
Total Posts: 22404
11 Apr 2014 08:10 PM
http://pastebin.com/drddDjQw
Report Abuse
AgentFirefox is not online. AgentFirefox
Top 100 Poster
Joined: 20 Jun 2008
Total Posts: 22404
11 Apr 2014 08:12 PM
You can ignore the Connection and Callbacks tables. They aren't used. I was going to implement custom events, but I realized you don't need them for what you wanted to do, and I forgot to remove those two lines from the script.

I hope you can understand what I did. :P
Report Abuse
notsopwnedg is not online. notsopwnedg
Joined: 07 Nov 2010
Total Posts: 4182
11 Apr 2014 08:22 PM
I understand most of it...Some of it took me a while to understand. But thanks. I never really thought of doing stuff this way.
Report Abuse
Previous Thread :: Next Thread 
Page 1 of 1
 
 
ROBLOX Forum » Game Creation and Development » Scripting Helpers
   
 
   
  • About Us
  • Jobs
  • Blog
  • Parents
  • Help
  • Terms
  • Privacy

©2017 Roblox Corporation. Roblox, the Roblox logo, Robux, Bloxy, and Powering Imagination are among our registered and unregistered trademarks in the U.S. and other countries.



Progress
Starting Roblox...
Connecting to Players...
R R

Roblox is now loading. Get ready to play!

R R

You're moments away from getting into the game!

Click here for help

Check Remember my choice and click Launch Application in the dialog box above to join games faster in the future!

Gameplay sponsored by:
Loading 0% - Starting game...
Get more with Builders Club! Join Builders Club
Choose Your Avatar
I have an account
generic image