How do i make the StringValue's value be what is in a TextBox?

I have a script:

local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function(Player)
	local ValueFolder = Instance.new("Folder")
	ValueFolder.Name = "ValueFolder"

	local ValueNickname = Instance.new("StringValue")
	ValueNickname.Name = "Nickname"
	ValueNickname.Value = Player.DisplayName
	ValueNickname.Parent = ValueFolder

	ValueFolder.Parent = Player
end)

and I want to make the Value = Text written in a textbox.
How should I change my code to make it do so?

The Text property represents the content of the TextBox. Therefore you can set the ValueNickname.Value property to TextBox.Text when the FocusLost event fires.

local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function(Player)
	local ValueFolder = Instance.new("Folder")
	ValueFolder.Name = "ValueFolder"

	local ValueNickname = Instance.new("StringValue")
	ValueNickname.Name = "Nickname"
	ValueNickname.Value = Player.PlayerGui.ScreenGui.Frame.NicknameFrame.NicknameBox.Text
	ValueNickname.Parent = ValueFolder

	ValueFolder.Parent = Player
end)

Like this?

Yes, that would set the nickname once. But I guess you want it to be set when the player types something new in the TextBox.
The FocusLost event fires when the player presses Enter for example or generally is done typing. You should connect a function to this event which updates the StringValue.

By the way, is that in a Script? This has to be in a LocalScript.

Yes exactly, but there is no apply edits button

Could u script it for me or tell me what I should add pls

It’s in a script in ServerScriptService

local textBox = ...
textBox.FocusLost:Connect(function()
    ValueNickname.Value = textBox.Text
end)

I should add this in what part of my script?

In that case you need to fire a RemoteEvent from the client to the server.

I’ll change it to LocalScript.
Where should the LocalScript be stored?

Create a RemoteEvent in ReplicatedStorage.

LocalScript in StarterGui:

local updateEvent = ...
local textBox = ...
textBox.FocusLost:Connect(function()
    updateEvent:FireServer(textBox.Text)
end)

Script in ServerScriptService:

local Players = game:GetService("Players")
local updateEvent = ...

Players.PlayerAdded:Connect(function(Player)
	local ValueFolder = Instance.new("Folder")
	ValueFolder.Name = "ValueFolder"

	local ValueNickname = Instance.new("StringValue")
	ValueNickname.Name = "Nickname"
	ValueNickname.Value = Player.DisplayName
	ValueNickname.Parent = ValueFolder

	ValueFolder.Parent = Player
end)

updateEvent.OnServerEvent:Connect(function(Player, Text)
	Player.ValueFolder.Nickname.Value = Text
end)

My friend tried it while I was still fixing some GUI’s and it doesn’t work, this comes in the output:

They are providing you sufficient code. Make sure to change the variable values to objects of your own.

So i should change updateEvent to the name of my remoteevent
and change textBox to the name of my textbox?

Yes change the variables to your directories.

1 Like