Maybe this is something you should consider (from the Roblox wiki):
Data stores can only be accessed by the server through
Scripts
. Attempting client-side access in aLocalScript
will cause an error.
Essentially, you will have to fire whatever string you have when the player leaves to the server (via a RemoteEvent), and let the server update the data and handle the datastore calls on PlayerAdded / PlayerRemoving and/or through a periodic saving system (as DataStore call rates are limited).
-- LocalScript
local remote = ur.remote.here
local textbox = script.Parent.Parent.TextBox
script.Parent.MouseButton1Click:Connect(function()
remote:FireServer(textbox.Text)
end)
-- ServerScript
local textInfo = {}
Players.PlayerAdded:Connect(function(player)
local success, data = pcall(function()
return DataStore:GetAsync(player.UserId)
end)
if success and data then
textInfo[player] = data
end
end)
local remote = ur.remote.here
remote.OnServerEvent:Connect(function(player, text)
if type(text) ~= "string" then
return
end
-- probably good idea to filter the text in case you need to display it
textInfo[player] = text
end)
-- Set up PlayerRemoving event to make sure that you save the text when the player leaves
-- You could also set up a periodic saving system if that's something you want to do
-- very basic example, you can build on this if you want
Players.PlayerRemoving:Connect(function(player)
local text = textInfo[player]
local success, err = pcall(function()
DataStore:SetAsync(player.UserId, text)
end)
if not success then
print(err)
end
end)