Simplify this script

hi, i need a help with this already working script. it choses the map based on a value inside replicated storage, and it must be like this, but i wonder if there is a quicker way to maker it work instead of inserting every map name
thank you

	if mostvoted.Value == "Aquarium" then
		chosenChapter= game.ReplicatedStorage.Chapters.Aquarium
	elseif mostvoted.Value == "Farm" then
		chosenChapter= game.ReplicatedStorage.Chapters.Farm
	elseif mostvoted.Value == "Mall" then
		chosenChapter= game.ReplicatedStorage.Chapters.Mall
	elseif mostvoted.Value == "School" then
		chosenChapter= game.ReplicatedStorage.Chapters.School
	elseif mostvoted.Value == "Desert" then
		chosenChapter= game.ReplicatedStorage.Chapters.Desert
	elseif mostvoted.Value == "Nebula" then
		chosenChapter= game.ReplicatedStorage.Chapters.Nebula
	elseif mostvoted.Value == "Crossroads" then
		chosenChapter= game.ReplicatedStorage.Chapters.Crossroads
	elseif mostvoted.Value == "Cottage" then
		chosenChapter= game.ReplicatedStorage.Chapters.Cottage
	elseif mostvoted.Value == "CandyLand" then
		chosenChapter= game.ReplicatedStorage.Chapters.CandyLand
	elseif mostvoted.Value == "Canyon" then
		chosenChapter= game.ReplicatedStorage.Chapters.Canyon
	elseif mostvoted.Value == "Hills" then
		chosenChapter= game.ReplicatedStorage.Chapters.Hills
	end
chosenChapter = game.ReplicatedStorage.Chapters:FindFirstChild( mostvoted.Value )
1 Like

Yeah, you could do it like this

chosenChapter = game.ReplicatedStorage.Chapters[mostvoted.Value]

That one line will be the same as all those lines.
It works because all of them have the same position in workspace, and the name of it is equal to the value of mostvoted

Edit: You can also use :FindFirstChild() over just the brackets, both work the same.

Could be stored in a table like that:

local ChaptersFolder = game:GetService("ReplicatedStorage").Chapters
local ChosenChapter = nil

local ChapterNameToInstance: {[string]: Instance} = {
["Aquarium"] = ChaptersFolder.Aquarium,
["Farm"] = ChaptersFolder.Farm,
["Mall"] = ChaptersFolder.Mall,
["School"] = ChaptersFolder.School,
["Desert"] = ChaptersFolder.Desert,
["Nebula"] = ChaptersFolder.Nebula,
["Crossroads"] = ChaptersFolder.Crossroads,
["Cottage"] = ChaptersFolder.Cottage,
["CandyLand"] = ChaptersFolder.CandyLand,
["Canyon"] = ChaptersFolder.Canyon,
["Hills"] = ChaptersFolder.Hills,
}

if table.find(ChaptersFolder, mostvoted.Value) then
	ChosenChapter = NameToInstance[mostvoted.Value]
end

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.