How to make TextBox text nil

Hi! I am making a custom ChatBot but there is one but in my script:

local chatInput = script.Parent:WaitForChild("Input")
local uis = game:GetService("UserInputService")
local player = game.Players.LocalPlayer
local ChannelValue = player:WaitForChild("ChannelValue", 8)
local DirectMessage = game.ReplicatedStorage:WaitForChild("Events"):WaitForChild("ChatBotMessage")
local CommandEvents = game.ReplicatedStorage:WaitForChild("ChatCommandEvents")
local cooldown = 0.05
local isCoolingDown = false
local player = game.Players.LocalPlayer

local admin 
if player.Name == "Actulurus" then admin = true else admin = false end
uis.InputEnded:Connect(function(key, gameProcessed)	
	if key.KeyCode == Enum.KeyCode.Slash and not gameProcessed then		
		chatInput:CaptureFocus()		
	end
end)


chatInput.FocusLost:Connect(function(enterPressed)
	if not enterPressed then return end
	local input = chatInput.Text
	chatInput.Text = ""	
	if string.len(input) > 0 then	
		if isCoolingDown == true then 		
			chatInput.Text = "Wait for cooldown to end!"		
			return 		
		end	
		isCoolingDown = true	
		print(input)
		DirectMessage:FireServer(input, "#ChatBot")
		script.Parent.Input:CaptureFocus()
		wait(cooldown)		
		isCoolingDown = false	
	end	
end)

CommandEvents.Sudo.OnClientEvent:Connect(function(msg)
	DirectMessage:FireServer(msg)		
end)

When I send my first message, the TextBox captures focus again but there is a space instead of no text.

I want to know how to make the text nil.

Thanks.

Can’t you just do

chatInput.Text = nil

No, It would return a ā€œstring expected, got nilā€ error

1 Like

No, it has to be a string you set the text to. Just setting it to "" should do the trick.

1 Like

That’s what I did, but instead of setting it to an empty string (without any spaces) it sets it to a string with one space.

That shouldn’t happen… Either way, you could try enabling the ClearTextOnFocus property instead.

It’s already enabled :confused: I tried everything.

Even tried string.sub(,0,0) and still returns a string with a space

Must be a bug then. Did you try restarting your device/studio?

Hate to say it but that’s pretty much the opposite

image

We’re looking for an issue with detecting when the TextBox's focus is lost

You’ll probably need to use some string patterns, or it just isn’t possible to set Properties as nil (Excluding Parent and such)

Not really, but I have been working on it for a few days (the entire game not just the bug), which means i restarted even my computer many times :smiley:

You can’t set .Text to nil, but you can make it empty - .Text = ""

None of that worked, Text = nil returned an error and empty string still contained a space

How it contained a space? May we know how you know it has a space in it?

When I set it equal to ā€œā€ it still contained a space. I could see the entire message ā€˜offset’ by one character at the start (which was the space")

It’s pretty much the same thing, clearing the text when the player clicks on the TextBox again, or after they press enter/click away. He’s clearing the text when the focus is lost, so that the TextBox is empty when the player clicks on it again vs the TextBox clearing itself when the player clicks on it.

image

Also, it works perfectly fine when i click away and then click on the TextBox again. The problem is i want to capture focus every time we send a message.

Just set the text to "" when the focus is captured.

after this line ^^^

Still not working :confused: i don’t know if this is a Roblox bug