Saving A String Values Value Via Data Stores

Hello,
I am making a test quest system and figured string values would help out. I am trying to figure out how to save the string values value with data stores. I also am trying to figure out how to make it so if say a event happened, it would change the string values value and save it. What I have so far isn’t working, and I am unsure how to approach this as there isn’t much material existing on this topic.
Data Store Script:

local dataStore = game:GetService("DataStoreService"):GetDataStore("Quests")


game.Players.PlayerAdded:Connect(function(plr)
local questext = Instance.new("StringValue", plr)
questext.Name = "Quest"

	local playerUserId = "Player_"..plr.UserId

	local data
	local success, errormessage = pcall(function()
		data = dataStore:GetAsync(tostring(playerUserId))
	end)

	if success then
		if data ~= nil then
			questext.Value = "Funny"
			print(questext.Value)
		else
			questext.Value = "No funny"
			print(questext.Value)


		end
	end

end)


game.Players.PlayerRemoving:Connect(function(plr)
	local playerUserId = "Player_"..plr.UserId

	local data = {plr.Character.Quest.Value}

	local success, errormessage = pcall(function()
		dataStore:SetAsync(tostring(playerUserId), data)
	end)

	if success then
		print("Data successfully saved.")
	else
		print("Error when saving data.")
		warn(errormessage)
	end
end)

Part Script Changing Value:

local click = script.Parent
click.MouseClick:Connect(function(Player)
Player.Character:FindFirstChild('Quest').Value = "Funny"
end)

I have a error on line 33 of the data store script saying lua ServerScriptService.Script:33: attempt to index nil with 'Quest'

it just means that Quest dosent exist

Okay but I still don’t understand how to fix this. I also don’t understand how i’d make it save the value of string value when it gets changed via the mouse click.

Quest isn’t in the character, but the player. Check where it’s parented to.

Just to remember, it is parented to the Player and not the character.
You can do it by:

repeat wait() until player.Character

local plrChar = player.Character
local quest = Instance.new("StringValue",plrChar)
quest.Name = "Quest"

Ah I see, lemme see if that fixes it.

image

image
can u spot the error?

local dataStore = game:GetService("DataStoreService"):GetDataStore("Quests")


game.Players.PlayerAdded:Connect(function(plr)
local questext = Instance.new("StringValue", plr)
questext.Name = "Quest"

	local playerUserId = "Player_"..plr.UserId

	local data
	local success, errormessage = pcall(function()
		data = dataStore:GetAsync(tostring(playerUserId))
	end)

	if success then
		if data ~= nil then
			questext.Value = "Funny"
			print(questext.Value)
		else
			questext.Value = "No funny"
			print(questext.Value)


		end
	end

end)


game.Players.PlayerRemoving:Connect(function(plr)
	local playerUserId = "Player_"..plr.UserId

	local data = {plr.Quest.Value}

	local success, errormessage = pcall(function()
		dataStore:SetAsync(tostring(playerUserId), data)
	end)

	if success then
		print("Data successfully saved.")
	else
		print("Error when saving data.")
		warn(errormessage)
	end
end)

Thanks, but I still am confused on how I’d get it to save the value when it gets changed by something like a mouse click.

	local playerUserId = "Player_"..plr.UserId

	local data = {plr.Quest.Value}

	local success, errormessage = pcall(function()
		dataStore:SetAsync(tostring(playerUserId), data)
	end)

	if success then
		print("Data successfully saved.")
	else
		print("Error when saving data.")
		warn(errormessage)
	end

paste this in the function BUT make sure that this is in the same script as the datastore

No need to do that, only save when you exit the game and/or save every 30 minutes in case of something happening. No need to save everytime you change value.

I tried this but for some reason its not saving the value the mouse click is giving. I was testing and changing the value to “Not funny 2” on the mouse click but when you go back in the game it just goes back to funny.

its cause ur changing the value locally change it serverly

I am not changing it locally, the mouse click is in a server script.

does it like show error or not?

Nope, just goes back to the value funny instead of loading Not funny 2.

So you are saying your data doesn’t get saved at all?

Its saving, but its not saving the correct value when you click the click detector. Just goes back to a different one. No clue why.

I’m having some trouble understanding your issue currently, sorry. Could you please explain?

Alright so. I have a click detector that is supposed to change the string values value. This works, but the data store script is not saving this value. It keeps printing as if the player has no existing data.

You have fixed the issue where it says Quest is nil right?