Saving and Loading Text through a Datastore

Hey,

I’ve been trying to send a textbox text through a datastore so that when the player joins back into the game it loads the same text they typed in before they left.

I haven’t been able to figure out why its not loading the data and I’ve been searching for a while but could not find why.

Server Script:

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

local SaveEvent = game.ReplicatedStorage.Remotes.ControlSave

SaveEvent.OnServerEvent:Connect(function(localPlayer, SaveData)
	local success,errormessage = pcall(function()
		local Key = localPlayer.UserId
		DataStore:SetAsync(Key, SaveData)	
		print("Set Async")	
	end)
	
	while not success do
		local Key = localPlayer.UserId
		print("Trying To Set Async")
		DataStore:SetAsync(Key, SaveData)
		print("Set Async")
		wait(3)
	end
end)

game.Players.PlayerAdded:Connect(function(localPlayer, SaveData)
	local Key = localPlayer.UserId
	
	local success, errormessage = pcall(function()
		local Data = DataStore:GetAsync(Key)
		
		if Data then
			SaveData = Data[1]
			print(SaveData)
		end	
	end)

	while not success do
		local Data = DataStore:GetAsync(Key)
		print("Working On it")

		if Data then
			SaveData = Data[1]
			print(SaveData)
			wait(2)
		end
	end
end)

Local Script:

local TextBox = script.Parent
local SaveEvent = game.ReplicatedStorage.Remotes.ControlSave

local function OnTextBoxFocusLost(EnterPressed, InputObject)
	if EnterPressed then
		print("Pressed Enter")

		local SaveData = TextBox.Text

		SaveEvent:FireServer(SaveData)

		print("Saved")
	end
end

TextBox.FocusLost:Connect(OnTextBoxFocusLost)

Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.

1 Like

if you are arent saving a table then you can just do SaveData = Data

I am assuming SaveData is nil? If that is the case, it is either because Data is not a table or the Data doesn’t have an element at index 1.

Could you possibly show the script on how the RemoteEvent Request is being handled on the server? This way we can help.

The two scripts are the only scripts I’m using but I think I figured out the issue and fixed it but thank you both

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.