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)
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:
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.
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.
Removed unnecessary semicolons at the end of lines.
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.
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
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.
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?
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
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)
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
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?
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)
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.