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

How do I make a model spawn when the player types in a word?

Asked by 1 year ago
Edited 1 year ago

I am trying to make a model spawn in when a player types in the model's name into a TextBox and presses an enter button.

The models' names are "apple" and "tree" and they are stored in ServerStorage. (I'm only trying to get the apple model to work right now.) I also have a remote event named JournalEvent in ReplicatedStorage.

I made two scripts, I wanted them to make the apple model appear when the player types "apple" into the TextBox (named "Write") and presses the ImageButton (named "EnterButton")

In StarterGui.JournalGui.JournalFrame I have a local script that says:

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

script.Parent.EnterButton.MouseButton1Click:Connect(function()
    for i, item in pairs(items) do
        if script.Parent.Write.Text == Nouns[1] then
        sound1:Play()   
        sound2:Play()
        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
        else
            script.Parent.Parent.FailMessage.Visible = true
            sound3:Play()
            wait(4)
            script.Parent.Parent.FailMessage.Visible = false
        item.Activated:Connect(function()
        local itemName = item.Name
        game.ReplicatedStorage.JournalEvent:FireServer(itemName, game.Players.LocalPlayer:GetMouse().hit.Position)

end)    
end
end
end)

In ServerScriptService I have a script that says:

local spawn = game.ReplicatedStorage.JournalEvent

spawn.OnServerEvent:Connect(function(plr, itemName, hitPos)
    local item = game.ServerStorage:FindFirstChild(itemName)
    if item then
        local itemClone = item:Clone()
        itemClone.Parent = game.Workspace
        itemClone:MoveTo(hitPos)
end
end)

When I run the game, type "apple" into the textbox, and press enter I get an error saying: Activated is not a valid member of TextLabel "Players.BunjiBloxxer.PlayerGui.JournalGui.JournalFrame.Title"

Edit: Idk how I got this error before, now when I run the exact same code I get an error saying: Activated is not a valid member of TextBox "Players.BunjiBloxxer.PlayerGui.JournalGui.JournalFrame.Write"

When I type anything else into the textbox and press enter, nothing happens (which is what I want)

How can I fix this? Thanks

1 answer

Log in to vote
1
Answered by 1 year ago
Edited 1 year ago

Activated is not a valid trigger from text labels. Your problem should be solved by converting the text labels to text buttons, as text buttons do have Activated triggers. In addition, you can add an if statement to double check that it actually is a TextButton you are trying to call Activated on.

LocalScript in the JournalGui

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

script.Parent.EnterButton.MouseButton1Click:Connect(function()
    for i, item in pairs(items) do
        if script.Parent.Write.Text == Nouns[1] then
            sound1:Play()   
            sound2:Play()
            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
        else
            script.Parent.Parent.FailMessage.Visible = true
            sound3:Play()
            wait(4)
            script.Parent.Parent.FailMessage.Visible = false
            if item.ClassName == "TextButton" then
                item.Activated:Connect(function()
                    local itemName = item.Name
                    game.ReplicatedStorage.JournalEvent:FireServer(itemName, game.Players.LocalPlayer:GetMouse().hit.Position)
                end)
            end
        end
    end
end)
0
Now I'm getting an error that says "Activated is not a valid member of TextBox "Players.BunjiBloxxer.PlayerGui.JournalGui.JournalFrame.Write" and I can't change the TextBox because I need the TextBox BunjiBloxxer 51 — 1y
0
Then you need to add an if statement in your loop to check if the item is a TextButton before checking the Activated trigger. The answer has been updated with the code sergeant_ranger 184 — 1y
0
I added this code, and I am no longer getting an error, but when I type in something that isn't "apple" and press "EnterButton" my "FailMessage" will play like normal, but instead of going away it just keeps re-appearing and playing sound 3 every couple seconds. Also my "apple" model still doesn't appear when I type "apple" and press "EnterButton" BunjiBloxxer 51 — 1y
Ad

Answer this question