How do I save a string value in my DataStore?

I understand how datastores work but the only thing I do not understand how to save a string. Here is my script and what I am trying to accomplish:

local dataStoreService = game:GetService("DataStoreService")
local chatcolorData = dataStoreService:GetDataStore("2013Color")

function onEnter(player)
local o = Instance.new("StringValue",player)
	o.Parent = player
	o.Name = "ChatColor"
	o.Value = chatcolorData:GetAsync(player.UserId) or "Red"
end

function onLeave(player)
	if player:FindFirstChild("ChatColor") then
		chatcolorData:SetAsync( player.UserId, player.ChatColor.Value )
	end
end

game.Players.PlayerAdded:Connect(onEnter)
game.Players.PlayerRemoving:Connect(onLeave)

Did you have any error in the Output ?

No, I did not have any error in the output.

To save a string value, you need to save the property that you need if I’m correct.
For example, let’s create a scenario where you require more than 1 property.
You can simply do this:

local table = {
script.Value.Value
script.Value.Parent
}

To reference it do this:

table[1]
table[2]

Unfortunately data stores cannot save instances and I assume it’s because of major crashes that will occur because of that. What’s fortunate though is that it can save properties since they are values. It’s able to save any values such as int values.

Did the StringValue is changed Server side or Client side ?

It is being changed Server sided

I just tried your script with adding 2 print to see if this was working and it work so i don’t know what you issue is :wink:

Setting the value

playerData:SetAsync(Key, {"Hello"})

Getting the value

local Table = playerData:GetAsync(Key)
local String = Table[1]
print(String)
1 Like