Roblox's split function sending nil output

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve?
    im trying to split code in text box to diffrent bits with space

  2. What is the issue?
    It just comes out as nil x 4 x 4
    image

  3. What solutions have you tried so far?
    I tried looking through dev forum and around internet everywhere

script.Parent.MouseButton1Click:Connect(function()
	local str = script.Parent.Parent.TextBox.Text
	local strTable = str:split(" ")
	for _,str in pairs(strTable) do
		print(str[1], str[2], str[3], str[4]) 
	end
end)

Could you show your hierarchy? More specifically: are you using a Script or a LocalScript?

TextBox input is client-sided so Scripts can’t pick up on any changes to the Text property of a TextBox from the client typing into it. TextBox input processing needs to be done with a LocalScript or a ModuleScript instance operating in a client context.

1 Like

image
local script:

script.Parent.FocusLost:Connect(function()

script.Parent.RemoteEvent:FireServer(script.Parent.Text)

end)

and the script:

script.Parent.RemoteEvent.OnServerEvent:Connect(function(player, text)
	script.Parent.Text = text
end)

Generally Scripts should never exist in any kind of Gui that’s placed in StarterGui (and by extension, PlayerGui); nor should any server-sided code be modifying any Guis being observed by the player. The RemoteEvent shouldn’t exist and this should be handled all by a LocalScript.

Refer to my previous post regarding why you’re getting nil. Assuming your TextBox defaults to blank (there’s nothing in the Text property if the player has not typed in it), Scripts do not pick up the changes made to the Text property by right of the player typing, so it appears blank. Can’t split nothing by something.

1 Like

the issue is it actualy does pick up the change i made it so it prints and it prints just fine


and the code to print it is

script.Parent.RemoteEvent.OnServerEvent:Connect(function(player, text)
	script.Parent.Text = text
	print(text)
	print(script.Parent.Text)
end)

Yes, the change is picked up because of the RemoteEvent setting the text - this is manual replication on your part, the property itself isn’t actually replicating otherwise.

There’s no point in the RemoteEvent solely for the purpose of setting text. Scripts and server-side code don’t belong in Guis. You can and should be handling this all from a LocalScript. Scripts and remotes living in a Gui like this is bad practice.

It’s much easier not to run into these kinds of esoteric problems if you structure your code correctly. Feedback is available in the past two posts I’ve sent.

1 Like

if i use local script it does not work at all

You need to do significantly more testing, debugging and learning here than just consuming what I’m saying and immediately volleying back whether it works or not. I’m not spoonfeeding you the answer here, there needs to be some effort to investigate this on your part.

I’ve done a barebones repro of your Gui’s hierarchy and split code and experience no such issues. Purely handled by a single LocalScript. It’s not enough to just change your script types.

Button.MouseButton1Click:Connect(function ()
	local str = string.split(Box.Text, " ")
	print(str, table.unpack(str))
end)
2 Likes

Thanks that actualy worked even tho i did keep it in script due to im trying to keep it server sided and it still worked perfectly fine.