Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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

✓ Accepted answer

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)

1 comment

0

Another question: is there a way to have the texbox lose its focus automatically?

This discussion is preserved as it was. New answers, comments, and votes are closed.