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

Is there an easier way to write this, without writing many times if I add more?

Asked by 4 years ago

Is there an easier way to write this, without writing many times if I add more? The models are from replicated storage

textBox:GetPropertyChangedSignal("Text"):Connect(function()
    if textBox.Text == "DropperOne" then
        placementModule:new("Dropper1", game.ReplicatedStorage.Models, plot.Plot, plot.PlacedObjects, false, true)
        textBox.Text = ""
        textBox.FocusLost = true        
    elseif textBox.Text == "DropperTwo" then
        placementModule:new("Dropper2", game.ReplicatedStorage.Models, plot.Plot, plot.PlacedObjects, false, true)
        textBox.Text = ""
        textBox.FocusLost = true
    elseif textBox.Text == "DropperThree" then
        placementModule:new("Dropper3", game.ReplicatedStorage.Models, plot.Plot, plot.PlacedObjects, false, true)
        textBox.Text = ""
        textBox.FocusLost = true        
    elseif textBox.Text == "Conveyor" then
        placementModule:new("Conveyor", game.ReplicatedStorage.Models, plot.Plot, plot.PlacedObjects, false, true)
        textBox.Text = ""
        textBox.FocusLost = true    
    end
end)

Thanks for the Help!

1 answer

Log in to vote
2
Answered by 4 years ago

There actually would be a way to shrink this using, as you might have guessed, dictionaries.

local texts = {
DropperOne = "DropperOne",
DropperTwo = "DropperTwo",
DropperThree = "DropperThree",
Conveyor = "Conveyor",
}
textBox:GetPropertyChangedSignal("Text"):Connect(function()
    if texts[textBox.Text] then -- check if it exists in our dictionary
        local val = texts[textBox.Text]; -- now that it exists, lets assign it's value to a variable
        placementModule:new((val), game.ReplicatedStorage.Models, plot.Plot, plot.PlacedObjects, false, true) -- use the ModuleScript with the value we got
        textBox.Text = ""
        textBox.FocusLost = true
    end
end)
0
Another question: is there a way to have the texbox lose its focus automatically? awesomemode14 68 — 4y
Ad

Answer this question