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: String saving

Previous Thread :: Next Thread 
ScriptOn is not online. ScriptOn
Joined: 22 Aug 2010
Total Posts: 10885
10 Jul 2013 05:07 PM
How do I encode something into string then decode it?

like encode(part.CFrame, part.Name)

then decode it? :O
Report Abuse
ScriptOn is not online. ScriptOn
Joined: 22 Aug 2010
Total Posts: 10885
10 Jul 2013 05:36 PM
bump
Report Abuse
ScriptOn is not online. ScriptOn
Joined: 22 Aug 2010
Total Posts: 10885
10 Jul 2013 05:56 PM
bump
Report Abuse
CodyTheBuildingKid is not online. CodyTheBuildingKid
Joined: 13 Dec 2011
Total Posts: 4399
10 Jul 2013 05:56 PM
        function Encode(CFrame, Name)
                local X, Y, Z = CFrame.X, CFrame.Y, CFrame.Z
                local StringCFrame = "CFrame.new("..tostring(X)..", "..tostring(Y)..", "..tostring(Z)..")"
                local Encoded = "["..StringCFrame.."]["..Name.."]"
                return Encoded
        end

        function Decode(String)
                local Cframe, Name
                for Match in String:gmatch("%[.-%]") do
                        local Match = Match:sub(2, Match:len() - 1)
                        if not Cframe then
                                Cframe = loadstring("return "..Match)()
                        elseif not Name then
                                Name = Match
                        end
                end
                return Cframe, Name
        end

        local EncodedVersion = Encode(CFrame.new(0, 0, 0), "Part")

        local DVCFrame, DVName = Decode(EncodedVersion)

        print(EncodedVersion)
        print(DVCFrame, DVName)

Output:
[CFrame.new(0, 0, 0)][Part]
0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1 Part

May not be the best way to do it, but this is the first thing to come to mind.
Report Abuse
ScriptOn is not online. ScriptOn
Joined: 22 Aug 2010
Total Posts: 10885
10 Jul 2013 05:58 PM
Thanks, i`m guessing I could just use this for LoadString and SaveString and it should work? (Obviously with some modding)
Report Abuse
CodyTheBuildingKid is not online. CodyTheBuildingKid
Joined: 13 Dec 2011
Total Posts: 4399
10 Jul 2013 05:59 PM
Yea just save the decoded version and when they come load it and decode it.
Report Abuse
ScriptOn is not online. ScriptOn
Joined: 22 Aug 2010
Total Posts: 10885
10 Jul 2013 06:00 PM
i`ll try messing with this right now, I`m going to edit it then reply and you can check over it?

I`m a bit of a newb with data persistence and encoding :P
Report Abuse
CodyTheBuildingKid is not online. CodyTheBuildingKid
Joined: 13 Dec 2011
Total Posts: 4399
10 Jul 2013 06:00 PM
Sure.
Report Abuse
ScriptOn is not online. ScriptOn
Joined: 22 Aug 2010
Total Posts: 10885
10 Jul 2013 06:00 PM
Thanks :)
Report Abuse
ScriptOn is not online. ScriptOn
Joined: 22 Aug 2010
Total Posts: 10885
10 Jul 2013 06:06 PM
Ok I have this for saving:


function save()
for i,v in pairs(shop:GetChildren()) do
for i,vv in pairs(v:GetChildren()) do
if vv.Name == "MainPart" then
--player:SaveNumber("Material" , material)

local Name = vv.Name
local CFrame = vv.CFrame
local X, Y, Z = CFrame.X, CFrame.Y, CFrame.Z
local StringCFrame = "CFrame.new("..tostring(X)..", "..tostring(Y)..", "..tostring(Z)..")"
local Encoded = "["..StringCFrame.."]["..Name.."]"
print(EncodedVersion)
end
end
end
end

shop.ChildAdded:connect(save)
shop.ChildRemoved:connect(save)
Report Abuse
ScriptOn is not online. ScriptOn
Joined: 22 Aug 2010
Total Posts: 10885
10 Jul 2013 06:08 PM
Ignore what I last said, look @ this:


function save()
local items = 0
for i,v in pairs(shop:GetChildren()) do
for i,vv in pairs(v:GetChildren()) do
if vv.Name == "MainPart" then
items = items+1
local Name = vv.Name
local CFrame = vv.CFrame
local X, Y, Z = CFrame.X, CFrame.Y, CFrame.Z
local StringCFrame = "CFrame.new("..tostring(X)..", "..tostring(Y)..", "..tostring(Z)..")"
local Encoded = "["..StringCFrame.."]["..Name.."]"
print(EncodedVersion)
local itemname = "item" ..items
player:SaveString(itemname, EncodedVersion)
end
end
end
end

shop.ChildAdded:connect(save)
shop.ChildRemoved:connect(save)
Report Abuse
CodyTheBuildingKid is not online. CodyTheBuildingKid
Joined: 13 Dec 2011
Total Posts: 4399
10 Jul 2013 06:11 PM
"print(EncodedVersion)" that would print nil because you named it 'Encoded'.

"local itemname = "item" ..items" just to be safe I like to do 'tostring(number)' when concatenating.

"player:SaveString(itemname, EncodedVersion)" same as above.
Report Abuse
ScriptOn is not online. ScriptOn
Joined: 22 Aug 2010
Total Posts: 10885
10 Jul 2013 06:13 PM
Is this OK?

function save()
local items = 0
for i,v in pairs(shop:GetChildren()) do
for i,vv in pairs(v:GetChildren()) do
if vv.Name == "MainPart" then
items = items+1
local Name = vv.Name
local CFrame = vv.CFrame
local X, Y, Z = CFrame.X, CFrame.Y, CFrame.Z
local StringCFrame = "CFrame.new("..tostring(X)..", "..tostring(Y)..", "..tostring(Z)..")"
local Encoded = "["..StringCFrame.."]["..Name.."]"
print(Encoded)
local itemname = "item" ..items
player:SaveString(itemname, Encoded)
end
end
end
end
Report Abuse
Notunknown99 is not online. Notunknown99
Joined: 05 Sep 2008
Total Posts: 25360
10 Jul 2013 06:14 PM
local StringCFrame = "CFrame.new("..tostring(X)..", "..tostring(Y)..", "..tostring(Z)..")"

How horrible.

local StringCFrame = 'CFrame.new('..tostring(CFrame)..')'
Report Abuse
CodyTheBuildingKid is not online. CodyTheBuildingKid
Joined: 13 Dec 2011
Total Posts: 4399
10 Jul 2013 06:14 PM
Just thought of another way.

function Encode(CFrame, Name)
local Encoded = { Cframe = {X = CFrame.X, Y = CFrame.Y, Z = CFrame.Z}, Name = Name }
Encoded = assert(LoadLibrary("RbxUtility")).EncodeJSON(Encoded)
return Encoded
end

--Now the CFrame and Name are in a table which was converted to a string

function Decode(String)
local Decoded = assert(LoadLibrary("RbxUtility")).DecodeJSON(String)
return CFrame.new(Decoded.Cframe.X, Decoded.Cframe.Y, Decoded.Cframe.Z), Decoded.Name
end

local Encoded = Encode(CFrame.new(0, 0, 0), "Part")
local DCFrame, DName = Decode(Encoded)
print(Encoded)
print(DCFrame, DName)
Report Abuse
ScriptOn is not online. ScriptOn
Joined: 22 Aug 2010
Total Posts: 10885
10 Jul 2013 06:16 PM
So is this OK?


function save()
local items = 0
for i,v in pairs(shop:GetChildren()) do
for i,vv in pairs(v:GetChildren()) do
if vv.Name == "MainPart" then
items = items+1
local Name = vv.Name
local CFrame = vv.CFrame
local X, Y, Z = CFrame.X, CFrame.Y, CFrame.Z
local StringCFrame = 'CFrame.new('..tostring(CFrame)..')'
local Encoded = "["..StringCFrame.."]["..Name.."]"
print(Encoded)
local itemname = "item" ..items
player:SaveString(itemname, Encoded)
end
end
end
end

shop.ChildAdded:connect(save)
shop.ChildRemoved:connect(save)
Report Abuse
CodyTheBuildingKid is not online. CodyTheBuildingKid
Joined: 13 Dec 2011
Total Posts: 4399
10 Jul 2013 06:16 PM
@Unknown, yea you could do that too. Sadly CFrame isn't supported with JSON, though.
Report Abuse
CodyTheBuildingKid is not online. CodyTheBuildingKid
Joined: 13 Dec 2011
Total Posts: 4399
10 Jul 2013 06:17 PM
Yea I think that'll work.
Report Abuse
ScriptOn is not online. ScriptOn
Joined: 22 Aug 2010
Total Posts: 10885
10 Jul 2013 06:17 PM
Ok so I use this to load:

function load()
local item = 0
for i = 1, 100 do
item = item+1
local itemname = "item"..item
local decodeme = player:LoadString(itemname)

local Cframe, Name
for Match in decodeme:gmatch("%[.-%]") do
local Match = Match:sub(2, Match:len() - 1)
if not Cframe then
Cframe = loadstring("return "..Match)()
elseif not Name then
Name = Match
end
end
end
end

load()


And then this to save:



function save()
local items = 0
for i,v in pairs(shop:GetChildren()) do
for i,vv in pairs(v:GetChildren()) do
if vv.Name == "MainPart" then
items = items+1
local Name = vv.Name
local CFrame = vv.CFrame
local X, Y, Z = CFrame.X, CFrame.Y, CFrame.Z
local StringCFrame = 'CFrame.new('..tostring(CFrame)..')'
local Encoded = "["..StringCFrame.."]["..Name.."]"
print(Encoded)
local itemname = "item" ..items
player:SaveString(itemname, Encoded)
end
end
end
end

shop.ChildAdded:connect(save)
shop.ChildRemoved:connect(save)







Is it good?;3
Report Abuse
Notunknown99 is not online. Notunknown99
Joined: 05 Sep 2008
Total Posts: 25360
10 Jul 2013 06:18 PM
local Encoded = { Cframe = {X = CFrame.X, Y = CFrame.Y, Z = CFrame.Z}, Name = Name }

Or you could do

local Encoded = { Cframe = {CFrame:components()}, Name = Name }

return CFrame.new(unpack(Decoded.Cframe))
Report Abuse
CodyTheBuildingKid is not online. CodyTheBuildingKid
Joined: 13 Dec 2011
Total Posts: 4399
10 Jul 2013 06:18 PM
In the load function you might want it to return the CFrame and name, or at least set whatever to them.
Report Abuse
Notunknown99 is not online. Notunknown99
Joined: 05 Sep 2008
Total Posts: 25360
10 Jul 2013 06:20 PM
@Cody: It doesnt need to be. I tostringed the CFrame, getting the 3 positional and 9 rotational(?) components.
Report Abuse
ScriptOn is not online. ScriptOn
Joined: 22 Aug 2010
Total Posts: 10885
10 Jul 2013 06:20 PM
function load()
local item = 0
for i = 1, 100 do
item = item+1
local itemname = "item"..item
local decodeme = player:LoadString(itemname)

local Cframe, Name
for Match in decodeme:gmatch("%[.-%]") do
local Match = Match:sub(2, Match:len() - 1)
if not Cframe then
Cframe = loadstring("return "..Match)()
elseif not Name then
Name = Match
end
end
local p = Instance.new("Part")
p.CFrame = Cframe
p.Name = Name
end
end


?

Btw will this work:


local StringCFrame = 'CFrame.new('..tostring(CFrame)..')'
Report Abuse
CodyTheBuildingKid is not online. CodyTheBuildingKid
Joined: 13 Dec 2011
Total Posts: 4399
10 Jul 2013 06:22 PM
Yea that looks good.
Report Abuse
ScriptOn is not online. ScriptOn
Joined: 22 Aug 2010
Total Posts: 10885
10 Jul 2013 06:30 PM
This is supposed to kill me upon loading and saving, but it doensn't. Here is the full code:

http://pastebin.com/xbf4vW40
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