Profile Service how do I add strings?

My idea is to add strings from a part or model name, I want to purchase a door and insert the name of the door into my players data like a simulator so when the player joins and they have purchased that door already, they can go through it.

Yes these both are module scripts in the “DataManager” Module script.

leaderstats scripts

local module = {}

function module:Create(player: Player, profile)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	local Diamond = Instance.new("IntValue")
	Diamond.Name = "Diamond"
	Diamond.Value = profile.Data.Diamond
	Diamond.Parent = leaderstats
	
	local Ruby = Instance.new("IntValue")
	Ruby.Name = "Ruby"
	Ruby.Value = profile.Data.Ruby
	Ruby.Parent = leaderstats
	
	local Bronze = Instance.new("IntValue")
	Bronze.Name = "Bronze"
	Bronze.Value = profile.Data.Bronze
	Bronze.Parent = leaderstats
	
	local Silver = Instance.new("IntValue")
	Silver.Name = "Silver"
	Silver.Value = profile.Data.Silver
	Silver.Parent = leaderstats
	
	local Multiplier = Instance.new("IntValue")
	Multiplier.Name = "Multiplier"
	Multiplier.Value = profile.Data.Multiplier
	Multiplier.Parent = leaderstats
	
end

return module

Template Script:

local Template = {
	Diamond = 0,
	Ruby = 0,
	Bronze = 0,
	Silver = 0,
	Multiplier = 1,
}

return Template

Incase you need the dataManager script:

local Players = game:GetService("Players")
local ServerScriptService = game:GetService("ServerScriptService")
local modules = ServerScriptService:WaitForChild("Modules")

local ProfileService = require(modules:WaitForChild("ProfileService"))
local Template = require(script:WaitForChild("Template"))
local leaderstats = require(script:WaitForChild("Leaderstats"))

local DataKey = "_DataStore"
local profileStore = ProfileService.GetProfileStore(DataKey, Template)

local datamanager = {}
datamanager.Profiles = {}

local function PlayerAdded(player: Player)
	local profile = profileStore:LoadProfileAsync("Player_"..player.UserId)
	
	if profile ~= nil then
		profile:AddUserId(player.UserId)
		profile:Reconcile()
		
		profile:ListenToRelease(function()
			datamanager.Profiles[player] = nil
			player:Kick()
		end)
		
		if player:IsDescendantOf(Players) then
			datamanager.Profiles[player] = profile
			leaderstats:Create(player, profile)
		else
			profile:Release()
		end
	else
		player:Kick()
	end
end

local function PlayerRemoving(player: Player)
	local profile = datamanager.Profiles[player]
	
	if profile ~= nil then
		profile:Release()
	end
end

for _, player in Players:GetPlayers() do
	task.spawn(PlayerAdded, player)
end

Players.PlayerAdded:Connect(PlayerAdded)
Players.PlayerRemoving:Connect(PlayerRemoving)

return datamanager

1 Like

So you want to add a string to datastore?
Best way to do this is by putting the string itself into a table

1 Like

so do I add is like this (Example) ?

local Template = {
	"Door1",
}
return Template

If not can you give me an example on where I can add the string values?

1 Like

yes I want to add strings that will save when the player rejoins!

1 Like

Mhm! I would also consider storying everything inside its own table, you can still put tables inside tables but saving multiple datastore keys is just… no.

example:


UserSaveData = {
	["leaderstats"] = {
		Diamond = 0,
		Ruby = 0,
		Bronze = 0,
		Silver = 0,
		Multiplier = 1,
	},
        ["DoorsUnlocked"] = {
         	"Door1"
     	}
}

How do I insert the StringValue into the script from a different script?

Bindable Event. Just like a remote event except it’s not used to connect the server and client

i got bored so ima give you an example script:

--Script 1

local DataToWrite = "My wife left me"
local bindableEvent = game.ReplicatedStorage.BindableEvent

function UpdateForScript2()
    bindableEvent:Fire(DataToWrite)
end
--Script 2

local RecievedData = nil 

function updateContent(content)
     RecievedData = content
end

game.ReplicatedStorage.BindableEvent.Event:Connect(updateContent)

Another method of storing values across scripts without values is with _G

_g.MyWife = "Nonexistant"

and now after the code above is ran, I can pick it up from another script!

print(_g.MyWife)