String Not Changing to Number

Hey DevForum! So I was working on something and was trying to make it so that, when your press next, it won’t let you forward unless the textbox text is a number.

productFunctions[1178702631] = function(receipt, player)
	player.PlayerGui.Music.SongRequest.Visible = true;
	
	player.PlayerGui.Music.SongRequest.Next.MouseButton1Click:Connect(function()
		local Box = player.PlayerGui.Music.SongRequest.TextBox.Text
		
		print(tonumber(Box))
		
		if tonumber(Box) then			
			player.PlayerGui.Music.SongRequest.Visible = false;
			
			table.insert(Requests, player.PlayerGui.Music.SongRequest.TextBox.Text)
			
			for _, values in pairs(Requests) do
			     print(values)
            end
		end
	end)
	
	return true;
end

When I print the tonumber(string) value, it prints nil, even if the textbox text are only made up of numbers. I tried doing more DevForum research, all of them doing basically the same method. Thanks.

1 Like

Make sure the text can be seen by the server at that moment. You need to send the text and process it through the server.

Happening because you’re trying to check the TextBox from a server script. Always why it’s a bad idea to try controlling Guis from a server script. Writing to a TextBox is client-sided input therefore only the client sees the new text in the TextBox.

You need a RemoteEvent. When the product is purchased, fire a RemoteEvent telling the client to make the song request Gui visible. The client, upon clicking the request button, should send the text of the TextBox to the server via RemoteEvent. The server can then validate (or sanitise) what the client has given it before inputting it into the queue.

1 Like

I don’t know if I understood properly but here is what I changed it to, according to what you were saying:

Client:

script.Parent.MouseButton1Click:Connect(function()

game.ReplicatedStorage.Events.SubmitRequest:FireServer(script.Parent.Text)

end)

game.ReplicatedStorage.Events.SubmitRequest.OnClientEvent:Connect(function()

script.Parent.Parent.Visible = false;

end)

Server:

local productFunctions = {}
local Requests = {}

productFunctions[1178702631] = function(receipt, player)
	player.PlayerGui.Music.SongRequest.Visible = true;

	return true;
end

game.ReplicatedStorage.Events.SubmitRequest.OnServerEvent:Connect(function(Player, Text)
	if tonumber(Text) then
		table.insert(Requests, tonumber(Text))
		
		game.ReplicatedStorage.Events.SubmitRequest:FireClient(Player)
	end
	
	for i, val in pairs(Requests) do
		print(val)
	end
	
end)

Sorry for the lack of context.

And is this giving you problems or…? You replied with new code but didn’t explain what’s happening lol. Can’t tell if you meant for this to be the solution or if it’s problematic. Context would be helpful.

I tried checking on the client like you said and then fire the text to the server.