You can name the script whatever you like, just make sure it is meaningful and easy to identify. For example, you could name it TextPersistenceScript
or DataStoreScript
.
will it affect my leaderstats datastore
No, this script will not affect your leaderstats datastore as it uses a separate data store with a unique identifier.
However, if you have other scripts that use the same data store as the one used in this script, then they may interact with the data this script is saving or loading. Itâs important to use unique identifiers for each data store to prevent any unwanted interactions.
i do not but r you for certain this should work
Yes, I am confident that this script should work, provided that you have filled in the correct data store name and that you have set up the data store correctly in your gameâs permissions.
One thing to note is that this script assumes that each player will have their own instance of the ScreenGui
. If youâre using this script for a game where players share a common ScreenGui
, then youâll need to adjust a few things.
Additionally, if you encounter any errors or issues, donât hesitate to ask for help.
the gui it changes globally for everyone thats how i want it
Got it, thanks for clarifying that. In that case, you donât need to make any changes to the script. The ScreenGui
will be displayed for all players and the text field values will be stored using the same data store for everyone.
But make sure you have set up the GUI in StarterGui
and that itâs called TextPersistenceGUI
. If you have named your GUI differently, then youâll need to adjust the script accordingly.
remember we named the screen gui to myscreengui
Oh, I apologize for the confusion. In that case, you would need to update the script to use the correct ScreenGui name. You can replace all instances of TextPersistenceGUI
in the script with myscreengui
.
Ok so what do i change im confused
Hereâs the updated script with the myscreengui
ScreenGui name:
local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local TextDataStore = DataStoreService:GetDataStore("TextDataStore")
local function saveText(player, text)
TextDataStore:SetAsync("Player_" .. player.UserId .. "_Text", text)
end
local function loadText(player)
local success, text = pcall(function()
return TextDataStore:GetAsync("Player_" .. player.UserId .. "_Text")
end)
if success then
return text or ""
else
return ""
end
end
local function setupGui(player)
local gui = Instance.new("ScreenGui")
gui.Name = "myscreengui"
gui.Parent = player:WaitForChild("PlayerGui")
local frame = Instance.new("Frame")
frame.Size = UDim2.new(0, 200, 0, 50)
frame.Position = UDim2.new(0.5, -100, 0.5, -25)
frame.BackgroundColor3 = Color3.new(1, 1, 1)
frame.BorderColor3 = Color3.new(0, 0, 0)
frame.Parent = gui
local label = Instance.new("TextLabel")
label.Size = UDim2.new(1, 0, 0.5, 0)
label.Position = UDim2.new(0, 0, 0.5, 0)
label.BackgroundTransparency = 1
label.TextColor3 = Color3.new(0, 0, 0)
label.TextWrapped = true
label.FontSize = Enum.FontSize.Size18
label.Text = "Enter your text:"
label.Parent = frame
local textBox = Instance.new("TextBox")
textBox.Size = UDim2.new(1, 0, 0.5, 0)
textBox.Position = UDim2.new(0, 0, 0.5, 0)
textBox.BackgroundColor3 = Color3.new(1, 1, 1)
textBox.BorderColor3 = Color3.new(0, 0, 0)
textBox.FontSize = Enum.FontSize.Size18
textBox.Parent = frame
textBox.Text = loadText(player)
textBox.FocusLost:Connect(function(enterPressed)
if enterPressed then
saveText(player, textBox.Text)
end
end)
end
Players.PlayerAdded:Connect(function(player)
setupGui(player)
end)
Make sure the name of the ScreenGui in StarterGui
is myscreengui
and it should work as intended.
so where do i place this
You can place this code into a ServerScript (not LocalScript) inside ServerScriptService
.
To create a new ServerScript, go to Game Explorer
> ServerScriptService
, right-click and select Insert Object
. Then, select Script
.
You can then copy and paste the code into the Script
object that was just created.
Hope this helps!
what abt the other scripts
You can keep the other scripts as they are. They donât need to be changed if they are working correctly.
Just make sure that all your scripts are inside ServerScriptService
(if they need to run on the server) or StarterPlayerScripts
(if they need to run on the client).
If youâre unsure about whether a script needs to be on the server or client, you can refer to this article on Roblox Developer Hub to learn more.
Ok can you give me the scripts i need and explain what to change
Sure, hereâs the list of scripts youâll need and what they do:
- Server Script: This script creates and manages the persistent data store.
local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local TextDataStore = DataStoreService:GetDataStore("TextDataStore")
local function saveText(player, text)
TextDataStore:SetAsync("Player_" .. player.UserId .. "_Text", text)
end
local function loadText(player)
local success, text = pcall(function()
return TextDataStore:GetAsync("Player_" .. player.UserId .. "_Text")
end)
if success then
return text or ""
else
return ""
end
end
Players.PlayerAdded:Connect(function(player)
player:WaitForDataReady()
local text = loadText(player)
player:SetAttribute("Text", text)
player:GetPropertyChangedSignal("Text"):Connect(function()
local newText = player:GetAttribute("Text")
if newText ~= text then
saveText(player, newText)
text = newText
end
end)
end)
In this script, you need to change the TextDataStore
name to the name of the data store you created earlier. You can also change the prefix of the keys in the saveText
and loadText
functions to suit your needs.
- Client Script: This script creates the GUI and displays it for the player.
local Players = game:GetService("Players")
local TextBox = script.Parent
local function onPlayerGui(playerGui)
if playerGui == nil then return end
local screenGui = playerGui:FindFirstChild("myscreengui")
if screenGui == nil then
screenGui = Instance.new("ScreenGui")
screenGui.Name = "myscreengui"
screenGui.Parent = playerGui
end
local frame = screenGui.Frame
if frame == nil then
frame = Instance.new("Frame")
frame.Size = UDim2.new(0, 200, 0, 100)
frame.Position = UDim2.new(0.5, -100, 0.5, -50)
frame.BackgroundColor3 = Color3.new(1, 1, 1)
frame.BorderColor3 = Color3.new(0, 0, 0)
frame.Parent = screenGui
local label = Instance.new("TextLabel")
label.Size = UDim2.new(1, 0, 0.5, 0)
label.Position = UDim2.new(0, 0, 0.5, 0)
label.BackgroundColor3 = Color3.new(1, 1, 1)
label.BackgroundTransparency = 1
label.TextColor3 = Color3.new(0, 0, 0)
label.TextWrapped = true
label.FontSize = Enum.FontSize.Size18
label.Text = "Enter your text:"
label.Parent = frame
TextBox.Size = UDim2.new(1, 0, 0.5, 0)
TextBox.Position = UDim2.new(0, 0, 0.5, 0)
TextBox.BackgroundColor3 = Color3.new(1, 1, 1)
TextBox.BorderColor3 = Color3.new(0, 0, 0)
TextBox.FontSize = Enum.FontSize.Size18
TextBox.Parent = frame
end
end
Players.LocalPlayer:WaitForChild("PlayerGui").ChildAdded:Connect(onPlayerGui)
onPlayerGui(Players.LocalPlayer:FindFirstChild("PlayerGui"))
In this script, you need to change the ScreenGui name, Frame size and TextLabel text to reflect the text entry size, label text to suit your needs.
- Client Script: This script updates the playerâs attribute whenever the text changes.
local TextBox = script.Parent
TextBox.FocusLost:Connect(function(enterPressed)
if enterPressed then
local player = game.Players.LocalPlayer
player:SetAttribute("Text", TextBox.Text)
end
end)
In this script, you donât need to make any changes.
Make sure that both the client scripts are located in StarterPlayerScripts
and named âGuiâ and âTextBoxâ respectively.
I hope that helps!
ok i already have the gui
Great! In that case, you can skip the second client script that I provided earlier.
You can add the first client script to StarterPlayerScripts
and change the ScreenGui name, Frame size and TextLabel text to suit your needs.
Make sure to keep both the Server Script
and the Client Script
inside ServerScriptService
.
i already have the gui why do i need a script for it?