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: Is their a way to save tables?

Previous Thread :: Next Thread 
blackhalodude is not online. blackhalodude
Joined: 16 Jun 2010
Total Posts: 412
24 Feb 2012 05:21 PM
localplayer = game.Players.LocalPlayer;
table = {};

for i,_ in pairs(localplayer.BackPack:GetChildren()) do
table[#table + 1] = i;
end

How would i save that?
Report Abuse
Ravenshield is not online. Ravenshield
Joined: 21 Nov 2007
Total Posts: 4143
24 Feb 2012 05:25 PM
It's in a localscript. Do you want it to be useable by the server (serverside)? Then you'd need to use a Script.

Anyway, for the record, I'd rather do:

localplayer = game.Players.LocalPlayer;
table = {};

for _, v in pairs(localplayer.Backpack:GetChildren()) do
table.insert(table, v)
end
Report Abuse
blackhalodude is not online. blackhalodude
Joined: 16 Jun 2010
Total Posts: 412
24 Feb 2012 05:29 PM
I want it to be useable by the server.
Report Abuse
Ravenshield is not online. Ravenshield
Joined: 21 Nov 2007
Total Posts: 4143
24 Feb 2012 05:41 PM
Then I'd suggest you use a Script, rather than a LocalScript.

You could save it as a global table.

_G.Users = {}

game.Players.PlayerAdded:connect(function(ply)
table.insert(_G.Users, ply.Name)
end)

-- In your other script that saves the backpack, unless you also code that into this script ^--

repeat wait(0.1) until _G.Users

local player = script.Parent.Parent -- Based on the script being put inside "PlayerGui". Just an example

local backpack = {};

for _, v in pairs(player.Backpack:GetChildren()) do
if v:IsA("Tool") or v:IsA("HopperBin") then
table.insert(backpack, v.Name)
end
end

table.insert(_G.Users[player.Name], backpack)

-- Inserts the table "backpack" into _G.Users under the player's name
It can be accessed by any script by then doing (example:

local name = player.Name
local indexOfG = nil

for i, v in pairs(_G.Users) do
if v == name then
indexOfG = i
break -- breaks the for-loop
end
end

for _, backpackItem in pairs(_G.Users[indexOfG]) do
print(backpackItem)
end

-- Should print all the items in "backpack", which is a 'subtable' of _G.Users[index].
Subtables are accessed by table[1][2]. Finds index #2 of the subtable of index #1 in 'table'


Report Abuse
Ravenshield is not online. Ravenshield
Joined: 21 Nov 2007
Total Posts: 4143
24 Feb 2012 05:41 PM
However!! I'm sure there are easier ways to do this. I tend to make things overcomplicated.
Report Abuse
blackhalodude is not online. blackhalodude
Joined: 16 Jun 2010
Total Posts: 412
24 Feb 2012 06:01 PM
So this script saves the tools in the backpack when you leave and then loads it back into backpack when you enter the game?
Report Abuse
blockoo is not online. blockoo
Joined: 08 Nov 2007
Total Posts: 17202
24 Feb 2012 06:03 PM
If you're just using it for numbers and strings, put the table in text form in a StringValue (or index the string with _G, if you prefer that). Then, to load the table into your script:

x = StringFormOfYourTable --i.e, _G.MyTable
loadstring(x)()
Report Abuse
blockoo is not online. blockoo
Joined: 08 Nov 2007
Total Posts: 17202
24 Feb 2012 06:08 PM
Here's an example code:

pseudoTable = "table = {"
for i = 1, 10 do
pseudoTable = pseudoTable..i..", "
end
pseudoTable = pseudoTable:sub(1, -3).."}" --remove the extra space and comma and add closing bracket
_G.SavedTable = pseudoTable

Then, to load it:

loadstring(_G.pseudoTable)()
print(table[3])

>3
Report Abuse
blackhalodude is not online. blackhalodude
Joined: 16 Jun 2010
Total Posts: 412
24 Feb 2012 06:08 PM
Oh. Well i just need a script that saves and loads tools on the backpack.
Report Abuse
blockoo is not online. blockoo
Joined: 08 Nov 2007
Total Posts: 17202
24 Feb 2012 06:09 PM
It should be applicable to that, too. Lemme edit it some...
Report Abuse
blackhalodude is not online. blackhalodude
Joined: 16 Jun 2010
Total Posts: 412
24 Feb 2012 06:10 PM
ok
Report Abuse
blockoo is not online. blockoo
Joined: 08 Nov 2007
Total Posts: 17202
24 Feb 2012 06:12 PM
pseudoTable = "weapons = {game.Lighting.Weapon1, game.Lighting.Weapon2}"
_G.SavedTable = pseudoTable

------------------------------------
loadstring(_G.SavedTable)()
weapons[1]:Clone().Parent = SomePlayer.Backpack

Should clone Weapon1 into the player's backpack.
Report Abuse
blackhalodude is not online. blackhalodude
Joined: 16 Jun 2010
Total Posts: 412
24 Feb 2012 06:15 PM
LOL, when i meant saving i meant saving so you get the same weapons when you join a game from the last time your where in the game.
Report Abuse
blockoo is not online. blockoo
Joined: 08 Nov 2007
Total Posts: 17202
24 Feb 2012 06:17 PM
Oh, that's even easier. Just put all the weapons you want saved in a model, then use SaveInstance/LoadInstance on the model. Or, if you wanted to save space, you could do it a more complicated way, but I doubt a model with a few weapons in it will take up a lot of space.
Report Abuse
blackhalodude is not online. blackhalodude
Joined: 16 Jun 2010
Total Posts: 412
24 Feb 2012 06:18 PM
Oh cool. lol i will tell you if i need any help :D
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