How do I put variables inside a string?

local TextBoxID = MainGui.TextBox.Text

TextButton.MouseButton1Up:Connect(function()
	local UsernameWeb = "https://api.rprxy.xyz/users/"..TextBoxID

My current code ^
User inputs a userid into the textbox then presses textbutton, the script proceeds to get the username (among other things) from the roblox API.

I know what I am doing, just don’t know the correct syntaxing, I’ve tried brackets etc. too.

2 Likes

I think you need use tostring().

Also I fixed the code for avoiding issues

local TextBoxID = MainGui.TextBox.Text

TextButton.MouseButton1Click:Connect(function()
   local UsernameWeb = "https://api.rprxy.xyz/users/".. tostring(TextBoxID)
   --do some stuff
end)
2 Likes

When you define the TextBox’s text as the variable your getting the text that is currently in the TextBox, instead you need to get the text whenever the button is pressed:

TextButton.MouseButton1Up:Connect(function()
    local TextBoxID = MainGui.TextBox.Text
	local UsernameWeb = "https://api.rprxy.xyz/users/"..TextBoxID
2 Likes

I am now having issues I don’t understand…


TextButton.MouseButton1Up:Connect(function()
	local TextBoxID = MainGui.TextBox.Text
	local UsernameWeb = "https://api.rprxy.xyz/users/".. tostring(TextBoxID)
	
	local function printUsername()
		local response
		local data
			
		pcall(function()
			response = HttpService:GetAsync(UsernameWeb)
			data = HttpService:JSONDecode(response)
		end)
		
		if not data then return false end
		
		if data.Id == (TextBoxID) then
			print(data.Username)
		end

*end)*

Getting the error where indicated.
'Expected identifier when parsing expression, got ‘)’

I have had this problem before. The issue is that you cannot put .Text in the TextBoxID Variable. I just programmed this and it works fine:

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

local UsernameWeb = "https://api.rprxy.xyz/users/"..TextBoxID.Text
1 Like

You did not close off one of your functions:

TextButton.MouseButton1Up:Connect(function()
	local TextBoxID = MainGui.TextBox.Text
	local UsernameWeb = "https://api.rprxy.xyz/users/".. tostring(TextBoxID)
	
	local function printUsername()
		local response
		local data
			
		pcall(function()
			response = HttpService:GetAsync(UsernameWeb)
			data = HttpService:JSONDecode(response)
		end)
		
		if not data then return false end
		
		if data.Id == (TextBoxID) then
			print(data.Username)
		end
    end -- this closes the printUsername function
*end)*

4 Likes

Thanks for the help guys, although it seems now my code isn’t printing the output. I am going to go back over the basics…

1 Like