Attempt to concatenate nil with string

I wanted to change my code from executing via button click to just pressing enter within the text box. The code however breaks when I make this change with the error “attempt to concatenate nil with string” on the line with the “!!!”.

original code

script.Parent.Join.MouseButton1Click:Connect(function()
	if Can then
		local Response,Delay = game.ReplicatedStorage.PrivateServers.Join:InvokeServer(script.Parent.TextBox.Text)
		if Response then
			script.Parent.ErrorMsg.Transparency = 0
			script.Parent.ErrorMsg.Text = Response
			Service:Create(script.Parent.ErrorMsg,TweenInfo.new(1,Enum.EasingStyle.Linear,Enum.EasingDirection.InOut,0,false,(Delay or 0) + 4),{Transparency = 1}):Play()
		end
		if Delay then
			wait(Delay)
		end
	end
end)

broken code

local textBox = script.Parent.TextBox

textBox.FocusLost:Connect(function(enterPressed, inputThatCausedFocusLost)
	if enterPressed then
		if Can then
		!!!!! local Response,Delay = game.ReplicatedStorage.PrivateServers.Join:InvokeServer(script.Parent.TextBox.Text)
			if Response then
				script.Parent.ErrorMsg.Transparency = 0
				script.Parent.ErrorMsg.Text = Response
				Service:Create(script.Parent.ErrorMsg,TweenInfo.new(1,Enum.EasingStyle.Linear,Enum.EasingDirection.InOut,0,false,(Delay or 0) + 4),{Transparency = 1}):Play()
			end
			if Delay then
				wait(Delay)
			end
		end
	else
		print("Player pressed", inputThatCausedFocusLost.KeyCode)
	end
end)
1 Like

Are you sure the error isn’t occurring on the server rather than the LocalScript invoking the RemoteFunction? I’m not seeing any concatenation in this LocalScript so I’m not sure what would be causing that error.

2 Likes

I don’t see how it could error since especially since the first block of code works

Can you show us the server sided code of the remote function?

It looks like the issue is arising from the line where you’re attempting to concatenate Response with other strings. The error message “attempt to concatenate nil with string” usually occurs when you’re trying to concatenate a string with a nil value.

To resolve this issue, you need to ensure that the Response variable has a valid value before trying to concatenate it with other strings. You can do this by checking if Response is not nil before attempting to use it.

Here’s the corrected code:

luaCopy code

local textBox = script.Parent.TextBox

textBox.FocusLost:Connect(function(enterPressed, inputThatCausedFocusLost)
	if enterPressed then
		if Can then
			local Response, Delay = game.ReplicatedStorage.PrivateServers.Join:InvokeServer(script.Parent.TextBox.Text)
			
			if Response then
				script.Parent.ErrorMsg.Transparency = 0
				script.Parent.ErrorMsg.Text = Response
				if Delay then
					wait(Delay + 4)
				end
				script.Parent.ErrorMsg.Transparency = 1
			end
			
			if Delay then
				wait(Delay)
			end
		end
	else
		print("Player pressed", inputThatCausedFocusLost.KeyCode)
	end
end)

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