Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Cannot store dictionary in datastore?

local module = require(script.DataStore)
local dataStore = game:GetService("DataStoreService"):GetDataStore("PlayerData")

game.ReplicatedStorage.Remotes.RemoveData.OnServerEvent:Connect(function(player)
	local key = player.UserId
	dataStore:RemoveAsync(key)
end)

local char = workspace.Character

game.ReplicatedStorage.Remotes.InputCharData.OnServerEvent:Connect(function(player, magicType)
	local dataa = module.makeTable(player)
	
	dataa.magic = magicType
	dataa.level = player.Database.Level.Value
	dataa.xp = player.Database.Level.XP.Value
	dataa.xpn = player.Database.Level.XPN.Value
	dataa.magicLevel = player.Database.MagicLevel.Value
	dataa.magicXP = player.Database.MagicLevel.MagicXP.Value
	dataa.magicXPN = player.Database.MagicLevel.MagicXPN.Value
	dataa.magicEnergy = player.Database.MagicEnergy.Value
	dataa.silver = player.Database.Silver.Value
	dataa.hp = player.Database.HP.Value
	dataa.hair1 = char.Hair.Handle.Mesh.MeshId
	dataa.hair2 = char.Hair2.Handle.Mesh.MeshId
	dataa.hair1Color = char.Hair.Handle.Mesh.VertexColor
	dataa.hair2Color = char.Hair2.Handle.Mesh.VertexColor
	dataa.hair1Offset = char.Hair.Handle.Mesh.Offset
	dataa.hair2Offset = char.Hair2.Handle.Mesh.Offset
	dataa.skinColor = char["Body Colors"].RightArmColor3
	dataa.face = char.Hed.Face.Texture
	dataa.shirt = char.Shirt.ShirtTemplate
	dataa.pants = char.Pants.PantsTemplate
	dataa.shirtColor = char.Shirt.Color3
	dataa.pantsColor = char.Pants.Color3
	
	game.ReplicatedStorage.Remotes.Teleport.OnServerEvent:Connect(function(player)	
		module.SaveData(player) -- here's problem	
		game:GetService("TeleportService"):Teleport(4398454020, player, dataa)
	end)
end)

SaveData function and makeTable function:

function module.makeTable(player)
	local data = {
		magic = player.Database.Magic.Value,
		magicLevel = player.Database.MagicLevel.Value,
		magicXP = player.Database.MagicLevel.MagicXP.Value,
		magicXPN = player.Database.MagicLevel.MagicXPN.Value,
		level = player.Database.Level.Value,
		xp = player.Database.Level.XP.Value,
		xpn = player.Database.Level.XPN.Value,
		silver = player.Database.Silver.Value,
		hp = player.Database.HP.Value,
		
		hair1 = player.Database.Hair1.Value,
		hair1Color = player.Database.Hair1.Hair1Color.Value,
		hair1Offset = player.Database.Hair1.Hair1Offset.Value,
		hair2 = player.Database.Hair2.Value,
		hair2Color = player.Database.Hair2.Hair2Color.Value,
		hair2Offset = player.Database.Hair2.Hair2Offset.Value,	
		skinColor = player.Database.SkinColor.Value,
		face = player.Database.Face.Value,
		shirt = player.Database.Shirt.Value,
		shirtColor = player.Database.ShirtColor.Value,
		pants = player.Database.Pants.Value,
		pantsColor = player.Database.PantsColor.Value,			 
	}
	
	return data
end

function module.SaveData(player)
	local key = player.UserId
	local data = module.makeTable(player)
	dataStore:SetAsync(key, data) -- line 37
end

The error: image Each value's type: image2

1 answer

✓ Accepted answer

You can't save color3 or vector3 values in datastores.

You can either use a function to save them as a string:

--convert to:
function convertToString(value)
	if typeof(value) == Vector3 or typeof(value) == Color3  then
		return value.X .. " " .. value.Y .. " " .. value.Z
	end
end

--convert back
function stringToVector3(string)
    local myTable = {}
	for c in string.gmatch(str,"%d+") do
           table.insert(tab, c)
	end
        return Vector3.new(myTable[1],myTable[2],myTable[3])
end

Or you can save them as a table:

myVector3 = Vector3.new(1,1,1)

toSave = {myVector3.X, myVector3.Y, myVector3.Z}
myVector3 = Vector3.new(myVector3.X, myVector3.Y, myVector3.Z)

20 comments

0

Can you print out the typeof() each value you're saving to the datastore?

0

done

0

I will wait. I am waiting for train and have nowhere to go.

0

heck. might have to team create if that's okay with you. I know it's an issue with saving something you're not supposed to but can't figure it out what that is. Would help if I could use Roblox's debugger and play around with it

0

ok sure.

0

did you accept friend request?

0

Yes

0

hang ooonnnnn gonna take a while with this crappy wifi

0

okay i can wait

0

see edit

0

thanks

0

yeah, no prob. if you need help converting them back let me know.

0

what is str and tab supposed to be? string and table?

0

Yes string and mytable, I changed the variable names and must've missed those. Might want to change the variable names to something other than just 'string' though since Roblox recognizes it as a keyword

0

how would i make a stringToColor3 function though

0

Same thing, but instead of return vector3.new(), return color3.new()

0

by the way, is it possible to just use tostring() and tonumber() instead of the functions?

0

no, sadly not :(

0

i get an error when i use stringToVector3() for hair1Color saying bad argument #1 to gmatch (string expected, got nil)

This discussion is preserved as it was. New answers, comments, and votes are closed.