Cluttered Tycoon Regen Script

Is there a way I can make this code less cluttered? It’s for a tycoon and it checks which coloured tycoon they had because all the tycoons are different and then replaces the tycoons with the correct colour tycoon.

Edit: OtherBase is a placeholder

if player.Base.Value == "BlueBase" then
			-- Create a new base
			local newBase = storage.BlueBase:clone()
			-- Move the clone into the world
			newBase.Parent = workspace
			-- Put the new base where the old base was
			newBase:SetPrimaryPartCFrame(location)
		elseif player.Base.Value == "OtherBase1" then
			-- Create a new base
			local newBase = storage.OtherBase1:clone()
			-- Move the clone into the world
			newBase.Parent = workspace
			-- Put the new base where the old base was
			newBase:SetPrimaryPartCFrame(location)
		elseif player.Base.Value == "OtherBase2" then
			-- Create a new base
			local newBase = storage.OtherBase2:clone()
			-- Move the clone into the world
			newBase.Parent = workspace
			-- Put the new base where the old base was
			newBase:SetPrimaryPartCFrame(location)
		elseif player.Base.Value == "OtherBase3" then
			-- Create a new base
			local newBase = storage.OtherBase3:clone()
			-- Move the clone into the world
			newBase.Parent = workspace
			-- Put the new base where the old base was
			newBase:SetPrimaryPartCFrame(location)
		end
2 Likes
function Put()
     local newBase = storage.Base:Clone()
     newBase.Parent = workspace
     newBase:SetPrimaryPartCFrame(location)
end

local BaseNames = {
     "BlueBase", "OtherBase1", "OtherBase2", "OtherBase3"
}

if table.find(BaseNames, player.Base.Value) then
     Put()
end
1 Like

Thanks for answering! I’ll try this and I’ll tell you if it works.

I haven’t tested it yet since I need to do stuff but wouldn’t that script just clone that 1 base instead of actually cloning the correct base?

Name them all “base”…number and in the script you can loop through all the bases like:

for i, v in ipairs(storage.Bases:GetChildren()) do
     if v.Name == "Base"..Base.Value then -- < int but you can do Base.Value if it's a string
          local v:Clone = v:Clone()
          clone:SetPrimaryPartCFrame(location)
     end
end)

Make sure they are in a folder of their own!

Isn’t this as efficient as my original code since I will still have to use elseif 3 more times?
Edit: Nevermind I didn’t understand it correctly.

I found a better fix today, thanks for the help guys though!

	-- Reset the base
	if player.Base.Value then
		
		-- Save the old base's location
		local location = player.Base.Value:GetPrimaryPartCFrame()
		
		-- Save the old base's name
		local baseName = player.Base.Value.Name
		
		-- Destroy the old base
		player.Base.Value:Destroy()
		
		-- Create a new base
		local newBase = storage:FindFirstChild(baseName):Clone()
		
		-- Move the clone into the world
		newBase.Parent = workspace
		
		-- Put the new base where the old base was
		newBase:SetPrimaryPartCFrame(location)
	end
	
end
game.Players.PlayerRemoving:Connect(savePlayerData)