I apologize for the confusion. In that case, you can modify the same loadText
and saveText
functions to load and save the entire ScreenGui and its children instead of just the MessageLabel.
Here’s how you can modify those functions:
local function loadScreenGui(player)
local success, value = pcall(function()
return MyDataStore:GetAsync(tostring(player.UserId))
end)
if not success then
warn("Failed to load Text for player " .. player.Name .. ": " .. tostring(value))
return nil
end
return value
end
local function saveScreenGui(player, screenGuiData)
local success, value = pcall(function()
MyDataStore:SetAsync(tostring(player.UserId), screenGuiData)
end)
if not success then
warn("Failed to save Text for player " .. player.Name .. ": " .. tostring(value))
end
end
Now, loadScreenGui
will return the entire ScreenGui and its children as a single object, and saveScreenGui
will save the entire ScreenGui object.
However, since the TextLabel inside your ScreenGui is named “TextLabel”, you still need to adjust the code to ensure that the correct TextLabel is being updated when a player’s text is changed. Here’s an updated version of the players.PlayerAdded
event to make sure the correct TextLabel is being updated:
players.PlayerAdded:Connect(function(player)
local screenGui = player.PlayerGui:FindFirstChild("MyScreenGui")
if not screenGui then
screenGui = Instance.new("ScreenGui")
screenGui.Name = "MyScreenGui"
screenGui.Parent = player.PlayerGui
local messageLabel = Instance.new("TextLabel")
messageLabel.Name = "TextLabel"
messageLabel.Position = UDim2.new(0.5, 0, 0.5, 0)
messageLabel.Size = UDim2.new(0, 200, 0, 50)
messageLabel.AnchorPoint = Vector2.new(0.5, 0.5)
messageLabel.BackgroundTransparency = 1
messageLabel.Font = Enum.Font.SourceSans
messageLabel.TextColor3 = Color3.new(1, 1, 1)
messageLabel.TextSize = 20
messageLabel.TextWrapped = true -- Allow text to wrap to multiple lines
messageLabel.Parent = screenGui
end
local screenGuiData = loadScreenGui(player)
if screenGuiData then
screenGui:ClearAllChildren()
for _, childData in ipairs(screenGuiData:GetChildren()) do
local child = Instance.new(childData.ClassName)
for property, value in pairs(childData:GetProperties()) do
if property ~= "Parent" then
child[property] = value
end
end
child.Parent = screenGui
end
end
player.Chatted:Connect(function(message)
if string.sub(message, 1, 11) == "-changetext" then
local newText = string.sub(message, 13)
screenGui.TextLabel.Text = newText
saveScreenGui(player, screenGui)
end
end)
end)
This version of the players.PlayerAdded
event will now get the existing ScreenGui for the player and load the data for that ScreenGui using loadScreenGui
. It will then clear all of the children of the ScreenGui and recreate them using the data that was loaded.
Finally, it will make sure to update the correct TextLabel object when a player’s text is changed.