| View previous topic :: View next topic |
| Author |
Message |
HexaG0n Advanced Cheater
Reputation: 0
Joined: 29 Mar 2021 Posts: 64
|
Posted: Sun Mar 06, 2022 1:36 am Post subject: insert table string path into table as object |
|
|
ok this is kinda hard for me to explain but, i want to insert a table path for example "clothing.head.hat" into table name: "player".
player is just a normal table: {} without anything in it.
i want the path convert player table into:
| Code: | -- from player = {} to \/\/
player = {
clothing = {
head = {
hat = {}
}
}
}
|
how can i do this? i've tried multiple ways but i still cant get it to work.
|
|
| Back to top |
|
 |
Frouk Grandmaster Cheater
Reputation: 5
Joined: 22 Jun 2021 Posts: 512
|
Posted: Sun Mar 06, 2022 2:12 am Post subject: |
|
|
| Code: | player = {
--Object
}
player.clothing.head.hat = "MODELS//HAT5.TXD"--ID or name |
|
|
| Back to top |
|
 |
HexaG0n Advanced Cheater
Reputation: 0
Joined: 29 Mar 2021 Posts: 64
|
Posted: Sun Mar 06, 2022 2:25 am Post subject: |
|
|
| Frouk wrote: | | Code: | player = {
--Object
}
player.clothing.head.hat = "MODELS//HAT5.TXD"--ID or name |
|
that will cause an error: attempt to index a nil value
well really thats why i made this thread
|
|
| Back to top |
|
 |
Dark Byte Site Admin
Reputation: 470
Joined: 09 May 2003 Posts: 25807 Location: The netherlands
|
Posted: Sun Mar 06, 2022 3:49 am Post subject: |
|
|
| Code: |
somestring='clothing.head.hat'
function addStringToTable(t,s)
local pieces=table.pack(s:split('.'))
for i=1,#pieces do
t[pieces[i]]={}
t=t[pieces[i]]
end
return t
end
player={}
addStringToTable(player,somestring)
player.clothing.head.hat = "MODELS//HAT5.TXD"--ID or name
|
_________________
Do not ask me about online cheats. I don't know any and wont help finding them.
Like my help? Join me on Patreon so i can keep helping |
|
| Back to top |
|
 |
HexaG0n Advanced Cheater
Reputation: 0
Joined: 29 Mar 2021 Posts: 64
|
Posted: Sun Mar 06, 2022 6:51 am Post subject: |
|
|
| Dark Byte wrote: | | Code: |
somestring='clothing.head.hat'
function addStringToTable(t,s)
local pieces=table.pack(s:split('.'))
for i=1,#pieces do
t[pieces[i]]={}
t=t[pieces[i]]
end
return t
end
player={}
addStringToTable(player,somestring)
player.clothing.head.hat = "MODELS//HAT5.TXD"--ID or name
|
|
thanks so much darkbyte, this works for me!
my code now:
| Code: | local function setTablePathValue(tbl, path, index, value)
for _,v in ipairs(path) do
tbl[v] = tbl[v] or {}
tbl = tbl[v]
end
index = (index or #tbl + 1)
tbl[index] = value
return tbl
end
local player = {}
setTablePathValue(player, {'clothing', 'head'}, 'hat', 'MODELS//HAT5.TXD') |
i just modified the function abit though, i think i might just use the key list right away instead of making a key string.
|
|
| Back to top |
|
 |
|