Then just save the value of the StringValue, as you can save strings to data stores.
local data = {
StringValue.Value --// If you have multiple values you need to save those inside a table like this
}
local success, result = pcall(function()
DataStoreService:UpdateAsync(Key, function()
return data
end)
end)
Yes, although, you would want to create the dictionary when you intend to save the WeaponSelection values to the data store. Creating it now may case trouble, as it won’t keep up to date with any changes done to the WeaponSelection values.
If you’re trying to save multiple pieces of data at once, then yes, you would need to create a dictionary for it. As for saving when the player dies, you can connect your save functions to fire whenever the player’s Humanoid has died.
Profile service is kind of hard to understand for beginners. It’s documentation is also confusing. I think suphis data store module is wayyy better and more performant than profile service. Suphis module also provides the same features as profile service such as session locking and many more
First of all watch the beginner tutorial video under the thread or just try to understand the post. After you’ve done this, you’ll need to open the players data store then set the date store’s value to your new data. Also, you need to destroy the players datastore when they leave. To get the data you can just use .find to get the players datastore and read its value
I just got online when I saw this but I’ll show you a simplified way of how I would do it.
I didn’t have time to comment on anything so just ask if you’re confused about something
Edit: I noticed I didn’t add loading the data onto the values so I added that. Also, removed unnecessary code.
local Players = game:GetService("Players")
local SDM = require(Path.To.Module)
local Template = {
Primary = "None",
Secondary = "None",
Melee = "None"
}
local function PlayerAdded(Player:Player)
local PlayerDS = SDM.new("Example", Player.UserId)
if PlayerDS:Open(Template) ~= "Success" then Player:Kick("Unable to load data!") return end
local function CreateStringValues(Name:string, Parent:Instance) : StringValue
local Uhh = Instance.new("StringValue")
Uhh.Name = Name
Uhh.Value = PlayerDS.Value[Name]
Uhh.Parent = Parent
local Connection:RBXScriptConnection;
Connection = Uhh.Changed:Connect(function(value: string)
if not PlayerDS.State then
if Connection then
Connection:Disconnect()
Connection = nil
end
return
end
PlayerDS.Value[Name] = value
end)
end
local Folder = Instance.new("Folder")
Folder.Name = "Folder"
Folder.Parent = Player
for I, X in pairs(Template) do
CreateStringValues(I, Folder)
end
end
local function PlayerRemoving(Player:Player)
local FoundDS = SDM.find("Example", Player.UserId)
if FoundDS then FoundDS:Destroy() end
end
Players.PlayerAdded:Connect(PlayerAdded)
Players.PlayerRemoving:Connect(PlayerRemoving)
In the loop you can pass a different parent for the stringValues created. This means you can delete the code that creates a new folder.
Does the code work for you?
Btw wanted to add this… If you change the string value in the folder during run time it will automatically save in the datatstore so you don’t have to worry about manually saving data when a player leaves
If you add new data then the next time you load your data table will grow. However, if you switch or delete those three keys, only the next player with no data will get the new updated data template applied as their data table. This means if you wanted to create a fresh new template with different keys then you’d have to create a new datastore or wipe everyone’s data.
Can you show me your original code with your string values? Because the way i did it, the changes to the string values will automatically apply to the keys within the players Datatstore value.
I just basically change the values without data stores
script.Parent.Event.OnServerEvent:Connect(function(player)
local WeaponTypeValue = player.Character:FindFirstChild("WeaponSelection").Melee
local WeaponName = script.Parent.Text
if WeaponTypeValue.Value ~= WeaponName then
if not player.Backpack:FindFirstChild(WeaponTypeValue.Value) then
player.Character:FindFirstChild(WeaponTypeValue.Value):Destroy()
else
player.Backpack:FindFirstChild(WeaponTypeValue.Value):Destroy()
end
WeaponTypeValue.Value = script.Parent.Text -- <- This changes the value
end
end)