Sending TextBox.Text to server via RemoteEvent not working

Hello. I’m trying to make a system where a player inputs a number between 1 to 100 and receives a note with the number they picked. Issue is that sending the number from the LocalScript to a ServerScript with a RemoteEvent isn’t going so well. It’s fair to say I dove into RemoteEvents a bit too fast but what can you do.

Here’s my code:
LOCAL SCRIPT:

local TextButton3 = script.Parent
local Players = game:GetService("Players").LocalPlayer.PlayerGui
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local GetNumberService = ReplicatedStorage:WaitForChild("LotteryNumberService")
local TextBox = Players.ChatBox1.Frame.TextBox

TextBox:GetPropertyChangedSignal("Text"):Connect(function()
	TextBox.Text = TextBox.Text:gsub('%D+', '');
	TextBox.Text = TextBox.Text:sub(1,3);
end)


script.Parent.MouseButton1Click:Connect(function()
	GetNumberService:FireServer(TextBox.Text)
end)

SERVER SCRIPT

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local TextBox = game.StarterGui.ChatBox1.Frame.TextBox
local numberService = ReplicatedStorage:WaitForChild("LotteryNumberService")
local LotteryCard = game:GetService("Lighting").LotteryCard -- this line is for later don't bother with it
local sendNumbertoServer = Instance.new("RemoteEvent")
sendNumbertoServer.Parent = ReplicatedStorage
sendNumbertoServer.Name = "LotteryNumberService"

local function onButtonPress()
	local playerNumber = tonumber(TextBox.Text)
	if type(tonumber(TextBox.Text)) == "number" then 
		if playerNumber >= 0 and playerNumber <= 100 then
			sendNumbertoServer:FireServer(playerNumber) [[ -- i'm pretty sure this shouldn't be here but i'm unsure on how to make the script fire when it's not inside the local script-- ]]
			print(playerNumber)
		end
	end
end

sendNumbertoServer.OnServerEvent:Connect(function(player, TextBox)
	onButtonPress()
	print(TextBox) -- checking if number has been received
end)

Any and all help is greatly appreciated.

4 Likes

I’m not sure why you’re running ReplicatedStorage:WaitForChild(“LotteryNumberService”) when underneath you create a remote named that

2 Likes

Thank you for that. Do you have any idea on how I would go about firing the onButtonPress function when the TextButton is pressed? Can I just add onButtonPressed underneath the sendNumbertoServer function connection?

I made some corrections on first script I’m not sure if my script is true. ( I don’t know your game variables or other scripts, objects in workspace.)

Here’s the corrected (?) code:

local TextButton3 = script.Parent
local Players = game:GetService("Players").LocalPlayer
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local GetNumberService = ReplicatedStorage:WaitForChild("LotteryNumberService")
local TextBox = Players.PlayerGui:WaitForChild("ChatBox1"):WaitForChild("Frame"):WaitForChild("TextBox")
TextBox:GetPropertyChangedSignal("Text"):Connect(function()  
  TextBox.Text = TextBox.Text:gsub('%D+', '') 
   TextBox.Text = TextBox.Text:sub(1,3)
end)
TextButton3.MouseButton1Click:Connect(function() 
   GetNumberService:FireServer(TextBox.Text)
end)

In the code above, I’ve made the following corrections:

  1. Changed game:GetService("Players").LocalPlayer.PlayerGui to game:GetService("Players").LocalPlayer.PlayerGui:WaitForChild("ChatBox1"):WaitForChild("Frame"):WaitForChild("TextBox") to access the TextBox inside the PlayerGui correctly.
  2. Updated the event connection from script.Parent.MouseButton1Click:Connect(function() to TextButton3.MouseButton1Click:Connect(function(), as TextButton3 is the local variable representing the script.Parent button.
  3. Removed unnecessary semicolons at the end of lines.
  4. Removed the redundant gsub line from TextBox:GetPropertyChangedSignal("Text"):Connect(function(), as it’s enough to only truncate the string to 3 characters using sub.
  5. Changed the local Players assignment to local Players = game:GetService("Players").LocalPlayer to access the LocalPlayer directly.
    Omg I’m so confused I’m sorry if I’m wrong
1 Like

Well, first of all the server should not be doing :FireServer but rather :FireClient or :FireAllClients

2 Likes

It might be useful for us maybe he forgot to share output I also don’t know what he wanted to achieve right here. I’ll be glad that if he shares about it. I understand him making a script is hard.

3 Likes

Nothing fires. What I’m trying to do is have the player input a number from 1 to 100 (the textbox should filter out any letters and allow 3 letters max) and sending that over to a client script. Everything works fine until I start mangling the script apart to attempt a RemoteEvent.
Here’s the working script pre-mangling:

local TextButton3 = script.Parent
local Players = game:GetService("Players").LocalPlayer.PlayerGui
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local TextBox = Players.ChatBox1.Frame.TextBox

TextBox:GetPropertyChangedSignal("Text"):Connect(function() 
	TextBox.Text = TextBox.Text:gsub('%D+', ''); -- allows only numbers 
	TextBox.Text = TextBox.Text:sub(1,3); -- allows only 3 letters
end)


script.Parent.MouseButton1Click:Connect(function()
local playerNumber = tonumber(TextBox.Text)
	if type(tonumber(TextBox.Text)) == "number" then -- convert string to number
		if playerNumber >= 0 and playerNumber <= 100 then  -- if it's a number from 0 to 100, run script below (this is where the :FireServer(text) should be I think)
			print(playerNumber)
		end
	end
end)

How would I go about converting this into a RemoteEvent?

3 Likes

I’ll just give you a fixed script.

Client:

local TextButton3 = script.Parent
local Players = game:GetService("Players").LocalPlayer.PlayerGui
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local GetNumberService = ReplicatedStorage:WaitForChild("LotteryNumberService")
local TextBox = Players.ChatBox1.Frame.TextBox

TextBox:GetPropertyChangedSignal("Text"):Connect(function()
	TextBox.Text = TextBox.Text:gsub('%D+', '');
	TextBox.Text = TextBox.Text:sub(1,3);
end)


script.Parent.MouseButton1Click:Connect(function()
	local number = GetNumberService:InvokeServer(TextBox.Text)
end)

Server:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local LotteryCard = game:GetService("Lighting").LotteryCard -- this line is for later don't bother with it
local sendNumbertoServer = Instance.new("RemoteFunction")
sendNumbertoServer.Parent = ReplicatedStorage
sendNumbertoServer.Name = "LotteryNumberService"

local function onButtonPress(text)
	local playerNumber = tonumber(text)
	if type(tonumber(text)) == "number" then 
		if playerNumber >= 0 and playerNumber <= 100 then
			return playerNumber
		end
	end
end

sendNumbertoServer.OnServerInvoke = function(player, TextBox)
	return onButtonPress(TextBox)
	--print(TextBox) -- checking if number has been received
end
2 Likes

It yields the RemoteEvent infinitely. Maybe somethings wrong with my explorer?
Here are some pictures:
image
image

1 Like

StarterGui is just a container for the gui and though it is available on the server, changes to it won’t have any effect on the clients.

1 Like

idk you weren’t getting the textbox properly in the first place

local TextButton3 = script.Parent
local Players = game:GetService("Players").LocalPlayer.PlayerGui
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local GetNumberService = ReplicatedStorage:WaitForChild("LotteryNumberService")
local TextBox = TextButton3.Parent.TextBox

TextBox:GetPropertyChangedSignal("Text"):Connect(function()
	TextBox.Text = TextBox.Text:gsub('%D+', '');
	TextBox.Text = TextBox.Text:sub(1,3);
end)


script.Parent.MouseButton1Click:Connect(function()
	local number = GetNumberService:InvokeServer(TextBox.Text)
end)
1 Like

I already changed that to game.PlayerGui

1 Like

Still won’t work because PlayerGui is neither a service of the game object nor a child. Instead each player has a child called PlayerGui which contained the UI

1 Like

Also elaborate your use case here? What is note? A tool?

1 Like

attempt to index nil with InvokeServer. Is it because the variables don’t transfer from script to script and I’d need to path the TextBox in the server script?

1 Like

Player inputs number to be set as their number on a note (it’s for a lottery rng type of thing)

1 Like

For some reason the remote doesn’t exist instead of creating it in a script try creating it in the explorer.

1 Like

Your client script seems fine but the server is iffy, what did you intend to do?

1 Like

I tried my luck:

(updated x3)

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local GetNumberService = ReplicatedStorage:WaitForChild("LotteryNumberService")

local TextBox = script.Parent.Parent.TextBox
local TextButton = script.Parent

TextBox:GetPropertyChangedSignal("Text"):Connect(function()
	TextBox.Text = TextBox.Text:gsub('%D+', '');
	TextBox.Text = TextBox.Text:sub(1,3);
end)


TextButton.MouseButton1Click:Connect(function()
	GetNumberService:FireServer(TextBox.Text)
end)
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local numberService = ReplicatedStorage:WaitForChild("LotteryNumberService")
local LotteryCard = game:GetService("Lighting").LotteryCard -- this line is for later don't bother with it
local sendNumbertoServer = Instance.new("RemoteEvent")
sendNumbertoServer.Name = "LotteryNumberService"
sendNumbertoServer.Parent = ReplicatedStorage

local function onButtonPress(player, text)
	local playerNumber = tonumber(text)
	if typeof(playerNumber) == "number" then 
		if playerNumber >= 0 and playerNumber <= 100 then
			--sendNumbertoServer:FireServer(playerNumber) [[ -- i'm pretty sure this shouldn't be here but i'm unsure on how to make the script fire when it's not inside the local script-- ]]
			print(playerNumber)
		end
	end
end

sendNumbertoServer.OnServerEvent:Connect(function(player, text)
	onButtonPress(player, text)
end)
4 Likes

Sending over the TextBox.Text to a server script and saving it as a variable that can be later on used on the paper note. Not sure that would even work but it’s a good guess. Either saving it as a variable or just giving me the ability to fetch it.

1 Like