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 » Scripters
Home Search
 

Re: Code for saving instances to datstores

Previous Thread :: Next Thread 
checkplate is not online. checkplate
Joined: 05 Jan 2016
Total Posts: 1379
26 Jan 2016 05:36 PM
I havent tested everything so this thats kinda why im dishing out the module. So you guys can test it for me and test bugs. Enjoy

local ReplicatedStorage = game:GetService'ReplicatedStorage'
local HttpService = game:GetService'HttpService'

local DataTypes = {
Lua = {
_string = true,
_number = true,
_boolean = true,
_userdata = true,
_nil = true,
_string = true,
},
Roblox = {
_Color3 = true,
_Vector3 = true,
_CFrame = true,
_BrickColor = true,
_BackgroundColor3 = true
}
}

local lib = {}

--- EncodeColor3, turns built-in color3 class into a JSON compatiable.
--- @para Color, the color 3 data you want to convert.
local function EncodeColor3(Color3)
local PsuedoClass = {
r = Color3.r,
b = Color3.b,
g = Color3.g
}

return PsuedoClass
end
lib.EncodeColor3 = EncodeColor3
lib.encodeColor3 = EncodeColor3

--- DecodeColor3, turns a psuedo JSON encode Color3 class to a Color3.
--- @para JSONColor3, the color3 psuedo class you want to decode.
local function DecodeColor3(JSONColor3)
local Class = Color3.new(
JSONColor3.r,
JSONColor3.b,
JSONColor3.g
)

return DecodeColor3
end
lib.DecodeColor3 = DecodeColor3
lib.decodeColor3 = DecodeColor3

local function EncodeBrickColor(BrickColor)
return BrickColor.Name
end

local function DecodeBrickColor(JSONBrickColor)
return BrickColor.palette(JSONBrickColor)
end

local function EncodeVector3(Vector3)
local PsuedoClass = {
x = Vector3.x,
y = Vector3.y,
z = Vector3.z
}

return PsuedoClass
end
lib.EncodeVector3 = EncodeVector3
lib.encodeVector3 = EncodeVector3

local function DecodeVector3(JSONVector3)
return Vector3.new(JSONVector3.x, JSONVector3.y, JSONVector3.z)
end

local TestPart = Instance.new'Part'
local TestGui = Instance.new'ImageLabel'

local function Set(Instance, Property, Value)
Instance[Property] = Value;
end

--- GetType, returns the type of a value, based off @elunates.
--- @para Value, the value you want to get the type of.
local function GetType(Value)
local Type = type(Value)

if Type ~= 'userdata' then return Type end

--- Routine for getting type goes as follows TestInstance > AttemptProperty with Value.
--- ... True > return property type.
--- ... False > keep testing properties.

if pcall(Set, TestPart, 'Position', Value) then
Type = 'Vector3'
elseif pcall(Set, TestPart, 'CFrame', Value) then
Type = 'CFrame'
elseif pcall(Set, TestPart, 'BrickColor', Value) then
Type = 'BrickColor'
elseif pcall(Set, TestGui, 'BackgroundColor3', Value) then
Type = 'Color3'
elseif pcall(Set, TestGui, 'Size', Value) then
Type = 'UDim2'
else
error('Type of ' .. Value .. ' is invaild or is not a testable type.')
end

return Type
end
lib.GetType = GetType

local function EncodeEntity(Type, Value)
return {Id = Type, Val = Value}
end

--- EncodeJSON, converts defualt lua data types as well roblox built in types.
--- @para Table, the table to encode into JSON format.
local function EncodeJSON(Table)
local ConvertedData = {}

for Index, Entry in next, Table do
local Type = GetType(Entry)
local EncodedEntry

if Type == 'Color3' then
EncodedEntry = EncodeColor3(Entry)
elseif Type == 'Vector3' then
EncodedEntry = EncodeVector3(Entry)
elseif Type == 'BrickColor' then
EncodedEntry = EncodeBrickColor(Entry)
elseif Type == 'CFrame' then
EncodedEntry = EncodeVector3(Entry)
elseif Type == 'table' then
EncodeJSON(Entry)
else
ConvertedData[Index] = Entry
end

if EncodedEntry then
ConvertedData[Index] = EncodeEntity(Type, EncodedEntry)
end
end

return HttpService:JSONEncode(ConvertedData)
end
lib.EncodeJSON = EncodeJSON

local function DecodeJSON(String)
local BasicDecode = HttpService:JSONDecode(String)
local FullDecode = {}

for Index, Entry in next, BasicDecode do
local Id = tostring(Entry.Id)

if type(Entry) == 'table' and DataTypes.Roblox['_' .. Id] then
local DecodedData

if Id == 'Color3' then
DecodedData = DecodeColor3(Entry)
elseif Id == 'Vector3' then
DecodedData = DecodeVector3(Entry)
elseif Id == 'BrickColor' then
DecodedData = DecodeBrickColor(Entry)
elseif Id == 'CFrame' then
DecodedData = DecodeVector3(Entry)
else
DecodedData = Entry
end

FullDecode[Index] = DecodedData
else
FullDecode[Index] = Entry
end
end

return FullDecode
end
lib.DecodeJSON = DecodeJSON

return lib

#code return cntkillme.happy > false
Report Abuse
lupine is not online. lupine
Joined: 24 Jun 2008
Total Posts: 3561
26 Jan 2016 05:46 PM
"#code return cntkillme.happy > false"
Savage.

Also this is neat. I'll check it out later tonight after my last meeting.
Report Abuse
SenseiWarrior is online. SenseiWarrior
Joined: 09 Apr 2011
Total Posts: 12140
26 Jan 2016 05:48 PM
You forgot UDim2 and NumberSequence and NumberRange and Vector2


Instance.new("BodyThrust",SenseiWarrior).position = CFrame.new(SenseiWarrior,YourGirlsDMs)
Report Abuse
checkplate is not online. checkplate
Joined: 05 Jan 2016
Total Posts: 1379
26 Jan 2016 06:17 PM
posting updated version

#code return cntkillme.happy > false
Report Abuse
checkplate is not online. checkplate
Joined: 05 Jan 2016
Total Posts: 1379
26 Jan 2016 06:34 PM
local ReplicatedStorage = game:GetService'ReplicatedStorage'
local HttpService = game:GetService'HttpService'

local Tabby = require(ReplicatedStorage:WaitForChild'TabbyEngine')
local Retrieve = Tabby.Retrieve

local TabbyUtils = Retrieve 'TabbyUtils'

local DataTypes = {
Lua = {
_string = true,
_number = true,
_boolean = true,
_userdata = true,
_nil = true,
_string = true,
},
Roblox = {
_Color3 = true,
_Vector3 = true,
_CFrame = true,
_BrickColor = true,
_BackgroundColor3 = true
}
}

local lib = {}

--- EncodeColor3, turns built-in color3 class into a JSON compatiable.
--- @para Color, the color 3 data you want to convert.
local function EncodeColor3(Color3)
local PsuedoClass = {
r = Color3.r,
b = Color3.b,
g = Color3.g
}

return PsuedoClass
end
lib.EncodeColor3 = EncodeColor3
lib.encodeColor3 = EncodeColor3

--- DecodeColor3, turns a psuedo JSON encode Color3 class to a Color3.
--- @para JSONColor3, the color3 psuedo class you want to decode.
local function DecodeColor3(JSONColor3)
local Class = Color3.new(
JSONColor3.r,
JSONColor3.b,
JSONColor3.g
)

return DecodeColor3
end
lib.DecodeColor3 = DecodeColor3
lib.decodeColor3 = DecodeColor3

--- EncodeBrickColor, turns built-in Brickcolor class into a JSON compatiable.
--- @para BrickColor, turns a brick color into a psuedo brick color class.
local function EncodeBrickColor(BrickColor)
return BrickColor.Name
end
lib.EncodeBrickColor = EncodeBrickColor
lib.encodeBrickColor = EncodeBrickColor

--- DecodeBrickColor, turns a psuedo JSON encode Color3 class to a Color3.
--- @para JSONBrickColor, the brick color name you want to decode.
local function DecodeBrickColor(JSONBrickColor)
return BrickColor.new(JSONBrickColor)
end
lib.DecodeBrickColor = DecodeBrickColor
lib.decodeBrickColor = DecodeBrickColor

--- EncodeBrickColor, turns built-in Brickcolor class into a JSON compatiable.
--- @para Vector3, the vector3 you want to convert into a vec3 psuedo class.
local function EncodeVector3(Vector3)
local PsuedoClass = {
x = Vector3.x,
y = Vector3.y,
z = Vector3.z
}

return PsuedoClass
end
lib.EncodeVector3 = EncodeVector3
lib.encodeVector3 = EncodeVector3

--- DecodeVector3, turns a vector3 psuedo class into a built-in Vector3
--- @para JSONVector3, the psuedo vector3 class you want to decode.
local function DecodeVector3(JSONVector3)
return Vector3.new(JSONVector3.x, JSONVector3.y, JSONVector3.z)
end
lib.DecodeVector3 = DecodeVector3
lib.decodeVector3 = DecodeVector3

do
local TestPart = Instance.new'Part'
local TestGui = Instance.new'ImageLabel'

local function Set(Instance, Property, Value)
Instance[Property] = Value;
end

--- GetType, returns the type of a value, based off @elunates.
--- @para Value, the value you want to get the type of.
local function GetType(Value)
local Type = type(Value)

if Type ~= 'userdata' then return Type end

--- Routine for getting type goes as follows TestInstance > AttemptProperty with Value.
--- ... True > return property type.
--- ... False > keep testing properties.

if pcall(Set, TestPart, 'Position', Value) then
Type = 'Vector3'
elseif pcall(Set, TestPart, 'CFrame', Value) then
Type = 'CFrame'
elseif pcall(Set, TestPart, 'BrickColor', Value) then
Type = 'BrickColor'
elseif pcall(Set, TestGui, 'BackgroundColor3', Value) then
Type = 'Color3'
elseif pcall(Set, TestGui, 'Size', Value) then
Type = 'UDim2'
else
error('Type of ' .. Value .. ' is invaild or is not a testable type.')
end

return Type
end
lib.GetType = GetType
end

local function EncodeEntity(Type, Value)
return {Id = Type, Val = Value}
end

--- EncodeJSON, converts defualt lua data types as well roblox built in types.
--- @para Table, the table to encode into JSON format.
local function EncodeJSON(Table)
local ConvertedData = {}

for Index, Entry in next, Table do
local Type = lib.GetType(Entry)
local EncodedEntry

if Type == 'Color3' then
EncodedEntry = EncodeColor3(Entry)
elseif Type == 'Vector3' then
EncodedEntry = EncodeVector3(Entry)
elseif Type == 'BrickColor' then
EncodedEntry = EncodeBrickColor(Entry)
elseif Type == 'CFrame' then
EncodedEntry = EncodeVector3(Entry)
elseif Type == 'table' then
EncodeJSON(Entry)
else
ConvertedData[Index] = Entry
end

if EncodedEntry then
ConvertedData[Index] = EncodeEntity(Type, EncodedEntry)
end
end

return HttpService:JSONEncode(ConvertedData)
end
lib.EncodeJSON = EncodeJSON

--- DecodeJSON, decodes a json with encoded psuedo classes
--- @para string, the json string coding.
local function DecodeJSON(String)
local BasicDecode = HttpService:JSONDecode(String)
local FullDecode = {}

for Index, Entry in next, BasicDecode do
if type(Entry) == 'table' and DataTypes.Roblox['_' .. Entry.Id] then
local Id = tostring(Entry.Id)
local Value = Entry.Val
local DecodedData

if Id == 'Color3' then
DecodedData = DecodeColor3(Value)
elseif Id == 'Vector3' then
DecodedData = DecodeVector3(Value)
elseif Id == 'BrickColor' then
DecodedData = DecodeBrickColor(Value)
elseif Id == 'CFrame' then
DecodedData = DecodeVector3(Value)
else
DecodedData = Entry
end

FullDecode[Index] = DecodedData
else
FullDecode[Index] = Entry
end
end

return FullDecode
end
lib.DecodeJSON = DecodeJSON


return lib

#code return cntkillme.happy > false
Report Abuse
checkplate is not online. checkplate
Joined: 05 Jan 2016
Total Posts: 1379
26 Jan 2016 06:38 PM
you can remove that retrieve thing at the top, that was something I was using.

#code return cntkillme.happy > false
Report Abuse
HuskiesPancake is not online. HuskiesPancake
Joined: 12 Apr 2013
Total Posts: 2623
26 Jan 2016 06:42 PM
You have to have the best siggy ever


#code print "Hello World!'
Report Abuse
Previous Thread :: Next Thread 
Page 1 of 1
 
 
ROBLOX Forum » Game Creation and Development » Scripters
   
 
   
  • 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