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: function string.bob()

Previous Thread :: Next Thread 
128GB is not online. 128GB
Joined: 17 Apr 2014
Total Posts: 8056
01 Nov 2014 06:20 PM
So if I wanna make a function in the 'string' table

function string.bob()
end -->error, Attempt to modify a readonly table

I get an error

If I do this

local string = {}
function string.bob()
end -->no error

print(string.sub("ok bye", 1, 2)) -->error, attempt to call field 'sub' (a nil value)
--Because I made string a blank table


If I do this
local string = (function() local tab = {} for x, f in pairs (string) do tab[x] = f end return tab end)()
function string.bob()
print("lol")
end -->no error

string.bob() -->lol
print(string.sub("ok bye", 1, 2)) -->ok


It completely works, but is there a better way?

I tried
local string = {unpack(string)}
But it didn't work because string is a dictionary table
Report Abuse
eLunate is not online. eLunate
Joined: 29 Jul 2014
Total Posts: 13268
01 Nov 2014 06:24 PM
Try using rawset. Personally, I have a helper function for creating readonly tables (I'd just sent a message to Jarod about it, because I'm helping him with OOP) and it works like this

function readonlytable(table)
return setmetatable({}, {
__index = table,
__newindex = function(table, key, value)
error("Attempt to modify read-only table")
end,
__metatable = false
});
end

Which makes a table that can only be set to with internal methods or rawset (Which bypasses the metatable)
This of course assumes that string was implemented in pure Lua hehe
Report Abuse
128GB is not online. 128GB
Joined: 17 Apr 2014
Total Posts: 8056
01 Nov 2014 06:28 PM
For the purpose of a module script, forgot to add
Idk if it matters



It would be like

local string = require(blah)
Report Abuse
eLunate is not online. eLunate
Joined: 29 Jul 2014
Total Posts: 13268
01 Nov 2014 06:35 PM
Can't you cheat by using require(module)() to override the scope's string table?
Report Abuse
128GB is not online. 128GB
Joined: 17 Apr 2014
Total Posts: 8056
01 Nov 2014 08:06 PM
I'm not sure what you mean (Because when I tried that it didn't work)
Report Abuse
eLunate is not online. eLunate
Joined: 29 Jul 2014
Total Posts: 13268
01 Nov 2014 08:07 PM
If you're not quite sure what I mean then honestly don't worry about it ^^
Report Abuse
128GB is not online. 128GB
Joined: 17 Apr 2014
Total Posts: 8056
01 Nov 2014 08:10 PM
I did this


workspace.module says

local module = {}
function module.bob()
print("bob")
end
return module



In the script

require(workspace.module())
module.bob() -->error, attempt to call a table value
Report Abuse
eLunate is not online. eLunate
Joined: 29 Jul 2014
Total Posts: 13268
01 Nov 2014 08:12 PM
require(workspace.module())
That's a new one to me...
Report Abuse
128GB is not online. 128GB
Joined: 17 Apr 2014
Total Posts: 8056
01 Nov 2014 08:14 PM
That was a typo sorry
I did require(workspace.module)()

Anyway I guess the way I did it isn't that bad anyway right?
Report Abuse
RoflBread is not online. RoflBread
Joined: 18 Jun 2009
Total Posts: 3803
01 Nov 2014 08:16 PM
Is it not

xxx = require(workspace.module)


xxx.bob()
Report Abuse
RoflBread is not online. RoflBread
Joined: 18 Jun 2009
Total Posts: 3803
01 Nov 2014 08:16 PM
3k post hype
Report Abuse
eLunate is not online. eLunate
Joined: 29 Jul 2014
Total Posts: 13268
01 Nov 2014 08:17 PM
Nah, it's not bad. I'd just have used rawset to import it though hehe
The deal is, when you go require(module)(), you're executing the function in a bit of a weird space. It inherits the function environment from the module's script, but it's within the environment of the scope that executed it, and with that you can (ab)use some things hehe
Report Abuse
GodShowsTheWay is not online. GodShowsTheWay
Joined: 27 Oct 2014
Total Posts: 7180
01 Nov 2014 08:17 PM
replace local strings with manipulated module values
Report Abuse
128GB is not online. 128GB
Joined: 17 Apr 2014
Total Posts: 8056
01 Nov 2014 08:17 PM
Yes it is but eLunate said
'Can't you cheat by using require(module)() to override the scope's string table?'

And they are right, there is something like that I've seen before
Anyway this is getting offtopic

Is there a more efficient or shorter (Or more efficient and slightly longer) way to do this

local string = (function() local tab = {} for x, f in pairs (string) do tab[x] = f end return tab end)()
Report Abuse
eLunate is not online. eLunate
Joined: 29 Jul 2014
Total Posts: 13268
01 Nov 2014 08:19 PM
Have you tried rawset, like I said?
Report Abuse
128GB is not online. 128GB
Joined: 17 Apr 2014
Total Posts: 8056
01 Nov 2014 08:20 PM
I don't know how
Report Abuse
GodShowsTheWay is not online. GodShowsTheWay
Joined: 27 Oct 2014
Total Posts: 7180
01 Nov 2014 08:20 PM
no try rawdelete
Report Abuse
eLunate is not online. eLunate
Joined: 29 Jul 2014
Total Posts: 13268
01 Nov 2014 08:22 PM
http://www.lua.org/pil/14.2.html

Will also give you a nice insight into changing the behaviour of the _G table
Report Abuse
GodShowsTheWay is not online. GodShowsTheWay
Joined: 27 Oct 2014
Total Posts: 7180
01 Nov 2014 08:23 PM
_f tables are bettetr
Report Abuse
eLunate is not online. eLunate
Joined: 29 Jul 2014
Total Posts: 13268
01 Nov 2014 08:24 PM
Get out you pratt
Report Abuse
GodShowsTheWay is not online. GodShowsTheWay
Joined: 27 Oct 2014
Total Posts: 7180
01 Nov 2014 08:24 PM
wow noob so rude be nice wow
Report Abuse
eLunate is not online. eLunate
Joined: 29 Jul 2014
Total Posts: 13268
01 Nov 2014 08:25 PM
This is the nicest you'll ever get to know me. Get used to it.
Report Abuse
GodShowsTheWay is not online. GodShowsTheWay
Joined: 27 Oct 2014
Total Posts: 7180
01 Nov 2014 08:26 PM
WHAT THE FUCK IS WRONG WITH YOU!?!?!
Report Abuse
RoflBread is not online. RoflBread
Joined: 18 Jun 2009
Total Posts: 3803
01 Nov 2014 08:27 PM
F bomb'd. Roblox has you now. Cya nerd.
Report Abuse
GodShowsTheWay is not online. GodShowsTheWay
Joined: 27 Oct 2014
Total Posts: 7180
01 Nov 2014 08:28 PM
DUMB PRICK GET THE FUCK OUT!!
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