Can't change my StringValue from a script

I am trying to make a GUI in a game where you put something in a textbox and then that message gets sent to a discord server

The problem is that the script at the bottom doesn’t work. What it’s supposed to do is change the value of a StringValue that stores the message, but the value of the StringValue doesn’t change

I have tried looking up solutions but the solution is always the same, and that solution doesn’t work

--Note: this script is stored in a TextBox
local value = game.Workspace.message --"message" is the name of my StringValue.

script.Parent.FocusLost:Connect(
	function(enterPressed)
		if enterPressed then
			value.Value = script.Parent.Text
		end
	end
)
2 Likes

Are you changing this value on a LocalScript? Changes made on the client (via a localscript) will not replicate to the server. You will need to utilize a Remote Event in order to communicate information between the client and the server (link here explains)

A general rule of thumb is to only use the client to obtain input and display information to a player. You should not be attempting to change server values on the client.

Can you clarify on this? What have you tried? What doesn’t work? Any errors?

1 Like

No, I’m changing it on a server script

The solution that wouldn’t work would be instead of doing something like this:

local value = game.Workspace.message.Value

script.Parent.FocusLost:Connect(
	function(enterPressed)
		if enterPressed then
			value = script.Parent.Text
		end
	end
)

You do something like this:

local value = game.Workspace.message

script.Parent.FocusLost:Connect(
	function(enterPressed)
		if enterPressed then
			value.Value = script.Parent.Text
		end
	end
)

Which hasn’t solved it. I haven’t tried anything else. And there are no errors that appear in the output when I join the game or put something in the TextBox

1 Like

Is the TextBox in your script part of a client-side GUI (StarterGui) or is it something on the server (SurfaceGui, etc)? Your script won’t work if it’s a server script that the server cannot see (descendent of a client service e.g. StarterGui, StarterPlayerScripts, etc)

1 Like

The TextBox is apart of a ScreenGui in the StarterGui

1 Like

Server Scripts do not execute any code if they’re not a descendent of a service that the server has access to. Only LocalScripts can run code in client-side services like StarterGui.
To fix your issue, change the script to a LocalScript while it’s under StarterGui, then create a Remote Event in ServerStorage so you can communicate between the client and the server. Get your input from the client like you’re doing now, but send that information to the server by firing your remote event from the client → server.

-- LocalScript in StarterGui
-- Obtains text input from the user and sends the information to the server
local myRemote = game:GetService("ServerStorage"):WaitForChild("MessageRemoteEvent")

script.Parent.FocusLost:Connect(function(enterPressed)
	if enterPressed then
		myRemote:FireServer(script.Parent.Text)
	end
end)
-- Script in ServerScriptService
-- Listens for the remote event to fire, updates the value passed from the client
local myRemote = game:GetService("ServerStorage"):WaitForChild("MessageRemoteEvent")
local message = game.Workspace.message

myRemote.OnServerEvent:Connect(function(playerWhoFired, value)
	print(playerWhoFired.Name .. " fired the remote event!")
	message.Value = value
end)
1 Like

the reason this didn’t work is because you wrapped all of your code in the function connection.

a simple fix would be the following:

--Note: this script is stored in a TextBox
local value = game.Workspace.message --"message" is the name of my StringValue.

script.Parent.FocusLost:Connect(function(enterPressed)
	if enterPressed then
		value.Value = script.Parent.Text
	end
end)
2 Likes

Both code snippets are the same, yours is just condensed into less lines.
You can add new lines in function parameters, such as when you’re declaring an anonymous function. Both code snippets will work.

1 Like

What do you mean by “message.Value = value”?

1 Like

From the LocalScript, you’re passing in the value of the Text property from the TextBox to your remote event when the focus is lost + enter is pressed. From the Server Script, you’re listening for the remote event to fire and updating the StringValue’s Value property on the server with the argument passed from the aforementioned LocalScript.
message is your StringValue in workspace. StringValues have a Value property, which accepts a string as the property’s value. We’re assigning the string passed in from the localscript to message’s Value property.

1 Like

I just realized what the answer is and I feel so stupid…

1 Like

The script still doesn’t work. Maybe it’s because the script that sends the message (that is right below) doesn’t fire the event.

local HS = game:GetService("HttpService")
local WebHook = --Discord webhook for my server
local textttttt = game.Workspace.message.Value
	
script.Parent.MouseButton1Click:Connect(function()
	local MessageData = {
		["content"] = textttttt
	}
	MessageData = HS:JSONEncode(MessageData)
	HS:PostAsync(WebHook, MessageData)
end)
1 Like