Saving StringValues with DataStore?

Hello devs! I am working on skin system for my game using StringValues and wanted this Value to save, when i tried DataStore StringValue, it showed, only IntValues can be saved. Here is my code :

local DataStore = game:GetService("DataStoreService")
local Data1 = DataStore:GetDataStore("Data1")

game.Players.PlayerAdded:Connect(function(player)
	local Data
	
	local stats = Instance.new("Folder")
	stats.Name = "leaderstats"
	stats.Parent = player
	
	local Survivals = Instance.new("IntValue")
	Survivals.Name = "Survivals"
	Survivals.Parent = stats
	
	local Died = Instance.new("BoolValue")
	Died.Name = "HasDied"
	Died.Parent = player
	
	local Died = Instance.new("BoolValue")
	Died.Name = "IsRake"
	Died.Parent = player
	
	local Skin = Instance.new("StringValue")
	Skin.Name = "Skin"
	Skin.Parent = player
	Skin.Value = "Rake"
	
	local Points = Instance.new("IntValue")
	Points.Name = "Points"
	Points.Parent = stats
	
	wait(0.1)
	
	local playerUserId = "Player_"..player.UserId
	
	local succes, errormessage = pcall(function()
		Data = Data1:GetAsync(playerUserId)
	end)
	
	if succes then
		Survivals.Value = Data.Survivals
	end
	
	game.ReplicatedStorage.PlayersInGame.Value = game.ReplicatedStorage.PlayersInGame.Value + 1
end)

game.Players.PlayerRemoving:Connect(function(player)
	local playerUserId = "Player_"..player.UserId
	local Data = {
		Survivals = player.leaderstats.Survivals.Value
	}
	local succes, errormessage = pcall(function()
		Data1:SetAsync(playerUserId, Data)
	end)
	if succes then
		print("Data saved succesfuly!")
	else
		warn(errormessage)
	end
	
	game.ReplicatedStorage.PlayersInGame.Value = game.ReplicatedStorage.PlayersInGame.Value - 1
end)

Create a dictionary with the same names as your values then when the player leaves or when you want to save the data go through all the values, put them in the dictionary, and save to the data store.

1 Like

Are you sure ?
You are trying to save a String while you initialize it as Int.
I’m sure there’s nothing wrong with the code but if you still want to save it as a String then use tostring() and tonumber()

Ok i made just one script that create the leaderstats and the StringValue

local ds = game:GetService("DataStoreService")
local valueDs = ds:GetDataStore("valueDs")


game.Players.PlayerAdded:Connect(function(player)
	local stats = Instance.new("Folder")
	stats.Name = "leaderstats"
	stats.Parent = player

	local stringValue = Instance.new("StringValue")
	stringValue.Value = "Test"
	stringValue.Parent = stats
	stringValue.Name = "StringValue"
	
	local data
	local success, errormessage = pcall(function()
		data = valueDs:GetAsync(player.UserId.."-stringValue")
	end)
	
	if success then
		stringValue.Value = data
	else
		print("Error while saving data")
		wait(errormessage)
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	
	local success, errormessage = pcall(function()
		valueDs:SetAsync(player.UserId.."-stringValue", player.leaderstats.StringValue.Value)
	end)
	
	if success then
		print("Data saved")
	else
		print("Error while saving data")
		warn(errormessage)
	end
end)
1 Like

Try changing this:

local Data = {
	Survivals = player.leaderstats.Survivals.Value
}

To this

local Data = {
	["Survivals"] = player.leaderstats.Survivals.Value
}```
1 Like