DataStore2 saves, but it doesn't change the value of a StringValue

I have created a personality trait system where the player chooses a trait, affecting the playstyle of that game I am making. I used DataStore2 to save a StringValue named “PersonalityTrait,” where when the player leaves, DataStore2 saves it and applies the trait they chosen to the StringValue. However, it does save but it doesn’t change the value of the StringValue. How would I fix this?

local DataStore2 = require(game.ReplicatedStorage.DataStore2)

DataStore2.Combine("wowness", "PersonalityTrait")

local defaultTrait = ""

function playerAdded(player)
	local leaderstats = Instance.new("Folder", player); leaderstats.Name = "leaderstats"
	local PersonalityTrait = Instance.new("StringValue", leaderstats);PersonalityTrait.Name = "PersonalityTrait"
	
	local dataTrait = DataStore2("PersonalityTrait", player)
	
	local function updateTrait(trait)
		game.ReplicatedStorage.RemoteEvents.UpdateTrait:FireClient(player, trait)
	end
	
	updateTrait(dataTrait:Get())
	
	dataTrait:OnUpdate(updateTrait)
end

game.Players.PlayerAdded:Connect(playerAdded)
for _,player in pairs(game.Players:GetPlayers()) do
   spawn(function() playerAdded(player) end)
end
2 Likes

Your usage of a remote event confuses me here. You have the string value, why not just use the :Get() on the string value instead of making it over-complex. Something like:

function playerAdded(player)
	local leaderstats = Instance.new("Folder", player); leaderstats.Name = "leaderstats"

    local dataTrait = DataStore2("PersonalityTrait", player)

	local PersonalityTrait = Instance.new("StringValue", leaderstats);PersonalityTrait.Name = "PersonalityTrait";PersonalityTrait.Value = dataTrait:Get()
end

Then for saving you simply save when the player leaves, and every amount of time. You may also want to add a function for when the game shutsdown, but I’m not currently in studio.

Oh, sorry about that. I forgot about putting information about the usage of the remote event. It is used for an indicator to see if it saved or not. Thank you for your response.

As long as you do the last 3 things it will pretty much always be saved. Unless its something uncontrollable.

Alright, thank you for that. So when a player joins with their selected “trait,” how would I get the data from the datastore to change the value of the StringValue?

In the script I made, I have PersonalityTrait.Value = dataTrait:Get(). This will automatically set the value for you. For saving, you use dataTrait:Set(PersonalityTrait.Value)

Thank you very much.

30 characters WOOOOOOOOOOO

Well, actually I just tried testing it out without the remote event and it didn’t work sadly. I did change the value of the StringValue through the server to the client to test it out but it doesn’t set the value of the StringValue.

You shouldn’t need to tell the client what the value of the StringValue is, since the StringValue is inside the player and the client can read it. Make sure you add the part of stuff about saving. Below what I added simply have

    game.Players.PlayerRemoved:Connect(function()
        dataTrait:Set(PersonalityTrait.Value)
    end

    while wait(200) do
        dataTrait:Set(PersonalityTrait.Value)
    end

You can also use :BindToClose() to save when the game shutsdown

Oh, alright. Thank you.

wowness

Wait, the value of the StringValue is blank even if there’s PersonalityTrait.Value = DataStore2:Get().

try PersonalityTrait.Value = DataStore2:Get(“None”)

Also you need to test it in Roblox, since Studio doesn’t save well

Ah, nevermind. I have fixed it, but thank you.