Why do Roblox textboxes always give a nil result? - lua

My problem is that I'm trying to get a custom input from the user in the form of TextBoxes. More specifically, I want to give players a chance to input feedback about my Roblox Game but the text from the textbox keeps being nil even when I type something into it. I expect it's probably an error on my part but I'm a tad unsure.
So currently, it looks like this on the client:
script.Parent.MouseButton1Click:Connect(function()
script.Parent.Visible = false
script.Parent.Parent.Feedback.Text = ""
wait(.1)
local success,nilmessage,Error = game.ReplicatedStorage.Feedback.SendFeedback:InvokeServer(script.Parent.Parent.Feedback.Text) --Invoking the server part.
print ("Message RAW = "..script.Parent.Parent.Feedback.Text)--Checking the text
--There's more stuff after here but this is the main part.
end)
The print statement always prints: Message Raw = and the server receives a nil value.
Any advice would be appreciated, if you need more, let me know.
Thanks for reading.

So as you say the print statement always prints Message RAW = ":
Here you assign an emtpy string:
script.Parent.Parent.Feedback.Text = ""
and here you concatenate and print it:
print ("Message RAW = "..script.Parent.Parent.Feedback.Text)
So the print that you observe is what is to be expected.
Why your server receives a nil value I cannot tell because you refuse to share the code on request.

Related

attempt to concatenate nil with string Roblox

I'm very confused. I'm trying to make it so that if you hold E with Roblox's "ProximityPrompt", you will get a GUI on your screen with some text. Everything works except that the text won't work. I also am not writing a string on the client script. There is a variable for it on the server script that gets passed over. But I keep seeing this error in the output.
Players.ford200000.PlayerGui.BuyGui.Frame.TextInput.Text:2: attempt to concatenate nil with string - Client -
Here is what I'm doing in my script
local sp = script.Parent
sp.ProximityPrompt.Triggered:Connect(function(player)
local name = sp.Name
local ss = game.ServerStorage
local item = ss.Hats:FindFirstChild(name)
local price = item.Price
game.ReplicatedStorage.ShopClickEvent:FireClient(player)
game.ReplicatedStorage.ShopInfoEvent:FireClient(player)
end)
And in the local script that listens for ShopInfoEvent
game.ReplicatedStorage.ShopInfoEvent.OnClientEvent:Connect(function(player, price, item)
script.Parent.Text = "Would you like to buy this ".. item.Name .." for ".. price.Value .."?"
end)
Please help, that would be greatly appreciated.
Your error is telling you that the object you are trying to add to a string is undefined.
This could be item.Name or price.Value is undefined and causing this string construction to fail.
Looking at how you are defining item and price shows that both of these values are undefined in your LocalScript's callback. When you call a RemoteEvent's FireClient function, the first argument tells the engine who to send the event to, and all the other arguments get passed in as the arguments of the callback. Currently, you aren't passing in any arguments at all.
So to fix your problem, you need to pass in the right arguments from the Script:
game.ReplicatedStorage.ShopInfoEvent:FireClient(player, price, item)
and parse them properly in your LocalScript :
game.ReplicatedStorage.ShopInfoEvent.OnClientEvent:Connect(function(price, item)
script.Parent.Text = "Would you like to buy this ".. item.Name .." for ".. tostring(price.Value) .."?"
end)
Players.ford200000.PlayerGui.BuyGui.Frame.TextInput.Text:2: attempt to
concatenate nil with string - Client -
tells you everything you need to know.
You're trying to concatenate nil with string. That means you're using the string concatenation operator .. with a nil operand in line 2
So let's look into line 2
script.Parent.Text = "Would you like to buy this ".. item.Name .." for ".. tostring(price.Value) .."?"
"Would you like to buy this ", " for " and "?" are obviously strings. That leaves us with item.Name and tostring(price.Value).
If price.Value were nil, tostring would turn it into "nil". So this cannot be the cause of this particular error message.
That leaves us with item.Name. If item were nil we would see an error for indexing a nil value instead. So that tells us that whatever item is, it is not what we expected to be. A table with an element for key "Name".
At this moment you know that something is wrong with the parameters of your function. So you (hopefully again) refer to the manual and compare that with the way you use those event functions.

attempt to index global 'message' (a nil value) Lua Message script

Currently, I'm working on a simple Lua Roblox script that is supposed to turn the parent part blue when "/blue" is entered in the chat by ANY player. When run, it returns the error "attempt to index global 'message' (a nil value)" in the output. Also, when I hover my cursor over "message" it says "unknown global 'message'". I am sure I'm doing something terribly wrong as I am new to the language. I have tried moving the script into Workspace and Chat (of course changing local part when I do) but those don't help. I'm confident it's a code issue specifically defining a global variable.
local part = script.Parent
local function scan()
if message:sub(1,5) == "/blue" then
part.BrickColor = BrickColor.Blue()
end
end
scan()
First, you didn't define "message" because "message" is supposed to be an argument of
player.Chatted()
So instead of just running scan(), make multiple functions, here is the revised code:
local part = script.Parent
game.Players.PlayerAdded:Connect(function(plr)
plr.Chatted:Connect(function(message)
message = string.lower(message)
if message == "/blue" then
part.BrickColor = BrickColor.new("Blue")
end
end)
end)
Let me know if you need me to elaborate, I understand that sometimes this stuff can be confusing.

How do I do "io.read" twice on one variable after inputting the wrong type?

Heyo. I'm pretty new to Lua (although I do code with Java), so I don't really know anything on this. I'm basically trying to get a user's input, and if it's not the right type, then restart. Now, I'm not sure if it's just Lua or my IDE (I'm using ZeroBrane Studio if that helps), but it won't reinput for whatever reason. (it just loops, meaning it skips the io.read line)
::restart::
...
a = io.read("*number")
if unit == nil then
print("Error! Incorrect Input!\nRestarting...")
goto restart
end
Oh, and yes, I'm using goto commands for restart. I thought that might be what's causing the issue, but I also tried this:
a = io.read("*number") --input non-number
print(a) --prints
a = io.read("*number") --skips
print(a) --prints
When you input a number, it doesn't skip.
Any help would be nice. Thanks in advance.
nvm i solved it myself
local a
repeat
a = io.read(); a = tonumber(a)
if not a then
print("Incorrect Input!\n(Try using only numbers)")
end
until a
::restart::
local a = io.read("*n", "*l")
if a == nil then
io.read("*l") -- skip the erroneous input line
print("Error! Incorrect Input!\nRestarting...")
goto restart
end
P.S.
Feel free to use goto whenever it makes your code more understandable.
For example, using while of repeat-until loop in this code wouldn't make it better (you would need either additional local variable or break statement).
Instead of using the built-in filter of io.read() (which I do think is bugged sometimes) you should consider using an own little function to ensure that the correct data will be give by the user.
This is such a function:
function --[[ any ]] GetUserInput(--[[ string ]] expectedType, --[[ string ]] errorText)
local --[[ bool ]] needInput = true
local --[[ any ]] input = nil
while needInput do
input = GetData()
if ( type(input) == expectedType ) then
needInput = false
else
print(errorText)
end
end
return input
end
You can then call it with:
local userInput = GetUserInput("number", "Error: Incorrect Input! Please give a number.")
Oh and on a sidenote: Goto is considered bad practice.

0 Checking if TextBox.Text contains the string in the table. But it doesn't work? Lua

I am making a script inside TextButton script that will check if the TextBox contains any of the word or string inside the table.
text = script.Parent.Parent:WaitForChild('TextBox')
label = script.Parent.Parent:WaitForChild('TextLabel')
a = {'test1','test2','test3'}
script.Parent.MouseButton1Click:connect(function()
if string.match(text.Text, a) then
label.Text = "The word "..text.Text.." was found in the table."
else
label.Text = "The word "..text.Text.." was not found in the table."
end
end)
But it gives an error string expected, got table. from line 7 which is refering to the line if string.match....
Is there any way to get all text in the table?
What's the right way to do it?
Oh boy, there's a lot to say about this.
The error message
Yes.
No, seriously, the answer is yes. The error message is exactly right. a is a table value; you can clearly see that on the third line of code. string.match needs a string as its second argument, so it obviously crashes.
Simple solution
use a for loop and check for each string in a separately.
found = false
for index, entry in ipairs(a) do
if entry == text.Text then
found = true
end
end
if found then
... -- the rest of your code
The better* solution
In Lua, if we want to know if a single element is in a set, we usually take advantage of the fact that tables are implemented as hashmaps, meaning they are very fast when looking up keys.
For that to work, one first needs to change the way the table looks:
a = {["test1"] = true, ["test2"] = true, ["test3"] = true}
Then we can just index a with a string to find out if it is contained int eh set.
if a[text.Text] then ...
* In practice this is just as good as the first solution as long as you only have a few elements in your table. It only becomes relevant when you have a few hundred entries or your code needs to run absolutely as fast as possible.

php str_replace produces strange results

I am trying to replace some characters in a text block. All of the replacements are working except the one at the beginning of the string variable.
The text block contains:
[FIRST_NAME] [LAST_NAME], This message is to inform you that...
The variables are defined as:
$fname = "John";
$lname = "Doe";
$messagebody = str_replace('[FIRST_NAME]',$fname,$messagebody);
$messagebody = str_replace('[LAST_NAME]',$lname,$messagebody);
The result I get is:
[FIRST_NAME] Doe, This message is to inform you that...
Regardless of which tag I put first or how the syntax is {TAG} $$TAG or [TAG], the first one never gets replaced.
Can anyone tell me why and how to fix this?
Thanks
Until someone can provide me with an explanation for why this is happening, the workaround is to put a string in front and then remove it afterward:
$messagebody = 'START:'.$messagebody;
do what you need to do
$messagebody = substr($messagebody,6);
I believe it must have something to do with the fact that a string starts at position 0 and that maybe the str_replace function starts to look at position 1.

Resources