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)
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.
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)