Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

Why is the if statement I made not working?

Asked by 2 years ago
Edited 2 years ago

I wrote an if statement that is supposed to make a model appear if someone types the correct word into a textbox and presses the enter button:

01local gui = script.Parent
02local frame = gui.JournalFrame
03local player = game.Players.LocalPlayer
04local sound = script.Parent.JournalFrame.X.JournalCloseSound
05local items = frame:GetChildren()
06local Nouns = {"apple", "tree"}
07local RemoteEvent = game.ReplicatedStorage.JournalEvent
08local sound1 = frame.Success
09local sound2 = frame.Book
10local sound3 = frame.Error
11 
12for i, item in pairs(items) do
13    if item:IsA("GuiButton") and frame.Write.Text == Nouns[1] then -- This is the if statement
14        item.Activated:Connect(function()
15            local itemName = item.Name
View all 36 lines...

When the person types "apple" into the textbox (named Write) and presses the enter button, it is supposed to spawn in the model, and if they write anything else and press enter, it is supposed to display the fail message, but even if they write "apple" it displays the fail message, why does this happen?

1 answer

Log in to vote
1
Answered by
amanda 1059 Moderation Voter
2 years ago
Edited 2 years ago

You are checking the contents of the TextBox whenever the script is first loaded. What you are intending to do is check the contents of the TextBox whenever the GuiButton is Activated.

I have not over analyzed or made any additional changes to your code aside from this: Inside the loop, I am declaring a function for each GuiButton found which will be called when that GuiButton is Activated. Inside that function I am checking the contents of the TextBox using your method and then running the appropriate code.

01local gui = script.Parent
02local frame = gui.JournalFrame
03local player = game.Players.LocalPlayer
04local sound = script.Parent.JournalFrame.X.JournalCloseSound
05local items = frame:GetChildren()
06local Nouns = {"apple", "tree"}
07local RemoteEvent = game.ReplicatedStorage.JournalEvent
08local sound1 = frame.Success
09local sound2 = frame.Book
10local sound3 = frame.Error
11 
12for i, item in pairs(items) do
13    if item:IsA("GuiButton") then
14        item.Activated:Connect(function()
15            if frame.Write.Text == Nouns[1] then
View all 36 lines...
0
Thanks for the help it worked! BunjiBloxxer 51 — 2y
Ad

Answer this question