Problem with TextBox script

I wanted to make a system, that requires a setup from the admin panel.

It is supposed to work like, an admin puts a number, which goes to the value, and it should be on the whole server, so I wanted to make a text that admin put visible for all users, but it did not work.

I tried server script, but it didn’t work since some functions should work in a local script, in a local script fuctions that should work in a normal script did not work.

I think I made a mistake in placing the script or something, here is the script.

–First create a RemoteEvent in ReplicatedStorage then:

–Client:
local textBox = … – Where your TextBox is
local textButton = … – Where your TextButton is

textButton.MouseButton1Click:Connect(function()
game.ReplicatedStorage:WaitForChild(“RemoteEvent”):FireServer(textBox.Text)
end)

–Server
game.ReplicatedStorage:WaitForChild(“RemoteEvent”).OnServerEvent:Connect(function(textBox)

game.ReplicatedStorage:WaitForChild(“RemoteEvent”):FireAllClients(textBox)

end)

–Client

game.ReplicatedStorage:WaitForChild(“RemoteEvent”).OnClientEvent:Connect(function(textBox)

textBox.Text = textBox

end)

1 Like

OnServerEvent actually takes two parameters, (1) the player who sent the message and (2) the message.

So your problem may be as simple as changing the function(textBox) to function(player, textBox).

I changed it and got this error.
image

I’m pretty sure the previous user is right about the function(textBox). I just decided to edit it for you.

local textBox = … – Where your TextBox is
local textButton = … – Where your TextButton is

textButton.MouseButton1Click:Connect(function()
game.ReplicatedStorage:WaitForChild(“RemoteEvent”):FireServer(textBox.Text)
end)

–Server
game.ReplicatedStorage:WaitForChild(“RemoteEvent”).OnServerEvent:Connect(function(plr, textBox)

game.ReplicatedStorage:WaitForChild(“RemoteEvent”):FireAllClients(textBox)

end)

–Client

game.ReplicatedStorage:WaitForChild(“RemoteEvent”).OnClientEvent:Connect(function(textBox)

textBox.Text = textBox

end)

Do I put it in 1 script or there should be more than 1?

There should be a server script and a local script.

Server script in ServerScriptsService, right?

Yes. (Maximum character limit)

I put them and got this error.
image

this is the error, you are trying to define a gui object as a string.

This is not working due to the fact you’re not using the Player parameter once listening to OnServerEvent (Mentioned by @bleintant) and you’re defining a string’s Text property - Which is not possible due to the fact strings don’t have one. (Mentioned by @Prespective_Broken)

The fix for this is to define your variables on the right way, define the TextBox’s text only if the Text is valid - On this case, If it’s an actual text and it has atleast 1 character or more and use the TextBox object instead of an actual string.

-- Server
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ChangeTextbox = ReplicatedStorage:WaitForChild("ChangeTextbox")

-- Listen to `ChangeTextbox` and fire back to all Clients.
ChangeTextbox.OnServerEvent:Connect(function(Player: Player, TextReceived: string)
	local IsValidText = TextReceived ~= nil and type(TextReceived) == "string"

	if not IsValidText then
		return
	end

	print("[Server] - Received text! Updating new Text to all Clients:", TextReceived)
	ChangeTextbox:FireAllClients(TextReceived)
end)

-- Client
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ChangeTextbox = ReplicatedStorage:WaitForChild("ChangeTextbox")

-- Replace those with the paths for your Instances.
local TextButton = script.Parent:WaitForChild("TextButton")
local TextBox = script.Parent:WaitForChild("TextBox")

-- Listen to whenever the Button gets fired.
TextButton.Activated:Connect(function()
	local Text = tostring(TextBox.Text)
	local Length = Text and string.len(Text)
	local IsValidLength = Length and Length > 0

	if not IsValidLength then
		return
	end

	-- It's not really recommended to fire strings into the Server;
	-- ... as they would cost some Networking bits/data.
	ChangeTextbox:FireServer(Text)
end)

-- Update `TextButton` once `ChangeTextbox` is fired to the Client.
ChangeTextbox.OnClientEvent:Connect(function(TextReceived: string)
	local IsValidText = TextReceived ~= nil and type(TextReceived) == "string"

	if not IsValidText then
		return
	end

	print("[Client] - Updating Local TextBox's Text to:", TextReceived)
	TextBox.Text = TextReceived
end)