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 1 year ago
Edited 1 year 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:

local gui = script.Parent
local frame = gui.JournalFrame
local player = game.Players.LocalPlayer
local sound = script.Parent.JournalFrame.X.JournalCloseSound
local items = frame:GetChildren()
local Nouns = {"apple", "tree"}
local RemoteEvent = game.ReplicatedStorage.JournalEvent
local sound1 = frame.Success
local sound2 = frame.Book
local sound3 = frame.Error

for i, item in pairs(items) do
    if item:IsA("GuiButton") and frame.Write.Text == Nouns[1] then -- This is the if statement
        item.Activated:Connect(function()
            local itemName = item.Name
            game.ReplicatedStorage.JournalEvent:FireServer(itemName, game.Players.LocalPlayer:GetMouse().hit.Position)
            player.PlayerGui.JournalGui.JournalFrame.Visible = false
            player.PlayerGui.MiniJournalGui.MiniJournalFrame.Visible = true
            player.PlayerGui.MiniJournalGui2.MiniJournalFrame2.Visible = false
            player.PlayerGui.JournalGui.JournalBackFrame.Visible = false
            player.PlayerGui.JournalGui.FailMessage.Visible = false
            sound:Play()
            wait(1)
            game.Players.LocalPlayer:GetMouse().Button1Down:Connect(function()
        end) 
    end) 
    elseif item:IsA("GuiButton") then 
        item.Activated:Connect(function()
            gui.FailMessage.Visible = true
            sound3:Play()
            wait(4)
            gui.FailMessage.Visible = false
            local itemName = item.Name
        end)
    end 
end 

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
1 year ago
Edited 1 year 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.

local gui = script.Parent
local frame = gui.JournalFrame
local player = game.Players.LocalPlayer
local sound = script.Parent.JournalFrame.X.JournalCloseSound
local items = frame:GetChildren()
local Nouns = {"apple", "tree"}
local RemoteEvent = game.ReplicatedStorage.JournalEvent
local sound1 = frame.Success
local sound2 = frame.Book
local sound3 = frame.Error

for i, item in pairs(items) do
    if item:IsA("GuiButton") then
        item.Activated:Connect(function()
            if frame.Write.Text == Nouns[1] then
                local itemName = item.Name
                game.ReplicatedStorage.JournalEvent:FireServer(itemName, game.Players.LocalPlayer:GetMouse().hit.Position)
                player.PlayerGui.JournalGui.JournalFrame.Visible = false
                player.PlayerGui.MiniJournalGui.MiniJournalFrame.Visible = true
                player.PlayerGui.MiniJournalGui2.MiniJournalFrame2.Visible = false
                player.PlayerGui.JournalGui.JournalBackFrame.Visible = false
                player.PlayerGui.JournalGui.FailMessage.Visible = false
                sound:Play()
                wait(1)
                game.Players.LocalPlayer:GetMouse().Button1Down:Connect(function()
                end)
            else
                gui.FailMessage.Visible = true
                sound3:Play()
                wait(4)
                gui.FailMessage.Visible = false
                local itemName = item.Name
            end
        end) 
    end
end
0
Thanks for the help it worked! BunjiBloxxer 51 — 1y
Ad

Answer this question