How do I have a player locally set a (textbox) value through a GUI and allow a serverscript to access it?

I’m in the process of making a simple banking system which reads the players cash & bank values and adds or subtracts to those values based on a GUI textbox input.

I have the textbox set to only allow numbers and the local script has no problem turning the text into a value using tonumber(GUI.Textbox.Text); except after I fire the remote event and the process gets handed to the server script, the textbox doesn’t update for the server script because the text is in a local GUI (I think?) and the server doesn’t have access to the local changes the user makes to the GUI. I tried firing the remote as well as defining its function inside the local script but wasn’t able to get that to work.

BankWithdraw.OnServerEvent:Connect(function(player, BankWithdraw)
	local Input = script.Parent.MainFrame.Input
		Bank.Value = Bank.Value - tonumber(Input.Text)
		Cash.Value = Cash.Value + tonumber(Input.Text)
end)

I thought having the server script inside the GUI would fix the issue but it doesn’t seem to. Hopefully I’m just woefully ignorant of a simple solution.

Send the input text thru the remote

BankWithdraw.OnServerEvent:Connect(function(player, BankWithdraw, InputText)
	local Input = script.Parent.MainFrame.Input
		Bank.Value = Bank.Value - tonumber(InputText)
		Cash.Value = Cash.Value + tonumber(InputText)
end)

And your way, players can type in any amount and have it subtracted or added regardless of how much they have. Add conditionals for security

1 Like

Local Script:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local remoteEvent = ReplicatedStorage:WaitForChild("BankWithdrawEvent")

local function onWithdrawButtonClicked()
    local inputText = script.Parent.MainFrame.Input.Text
    local amount = tonumber(inputText)

    if amount then
        remoteEvent:FireServer(amount)
    else
        print("Invalid input")
    end
end

script.Parent.MainFrame.WithdrawButton.MouseButton1Click:Connect(onWithdrawButtonClicked)

Serverscript example:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local remoteEvent = Instance.new("RemoteEvent")
remoteEvent.Name = "BankWithdrawEvent"
remoteEvent.Parent = ReplicatedStorage

remoteEvent.OnServerEvent:Connect(function(player, amount)
    -- Validate and process the input on the server
    local Bank = player:WaitForChild("Bank")
    local Cash = player:WaitForChild("Cash")

    if typeof(amount) == "number" then
        Bank.Value = Bank.Value - amount
        Cash.Value = Cash.Value + amount
    else
        warn("Invalid input received from player " .. player.Name)
    end
end)
3 Likes

(To OP) That’s not how remote events work. Your code shows you accessing a remote event function however it’s still trying to get input from the UI itself and I assume your script is in the UI itself (which when a player changes text in a box on the UI, that change will not replicate to the server). You should try keep all server scripts in ServerScriptService and RemoteEvents in ReplicatedStorage in folders for organisation and optimisation.

You need to fire this amount to the server from the client first:

Person above beat me to it but yeah they do a good job with the code provided.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.