How could i possibly make this script shorter and better?

so recently ive been doing a script that with the click of a button changes the scheme of a car’s body depending on its given value.

thing is, im doing it the very old and bad/overcomplicated way since i dont know how to properly optimize it,
which in turn is making me lose a LOT of time just organizing the schemes + making it appear in the rotation.
heres the script:

-- Player
local Plr = script.Parent.Parent.Parent.Parent.Parent.Parent.Name

-- Gets the player car
local Car = game.Workspace.Cars:WaitForChild(Plr.. "Car")

-- Scheme Value
local Value = script.Parent.SchemeValue

-- The car body parts
local Body = Car:FindFirstChild("Body")

-- Scheme List
local Scheme0 = "rbxassetid://11170714394"
local Scheme1 = "rbxassetid://11379831515"
local Scheme2 = "rbxassetid://11229241277"
local Scheme3 = "rbxassetid://11229244214"
local Scheme4 = "rbxassetid://11379843052"
local Scheme5 = "rbxassetid://11379861502"
local Scheme6 = "rbxassetid://11379862505"
local Scheme7 = "rbxassetid://11379863968"
local Scheme8 = "rbxassetid://11229247573"

script.Parent.SchemeValue.Changed:Connect(function()
	if Value.Value == 0 then
		for i, v in pairs(Body:GetDescendants()) do
			if v.ClassName == "MeshPart" then
				v.TextureID = Scheme0
			end
		end
	elseif Value.Value == 1 then
		for i, v in pairs(Body:GetDescendants()) do
			if v.ClassName == "MeshPart" then
				v.TextureID = Scheme1
			end
		end
	elseif Value.Value == 2 then
		for i, v in pairs(Body:GetDescendants()) do
			if v.ClassName == "MeshPart" then
				v.TextureID = Scheme2
			end
		end
	elseif Value.Value == 3 then
		for i, v in pairs(Body:GetDescendants()) do
			if v.ClassName == "MeshPart" then
				v.TextureID = Scheme3
			end
		end
	elseif Value.Value == 4 then
		for i, v in pairs(Body:GetDescendants()) do
			if v.ClassName == "MeshPart" then
				v.TextureID = Scheme4
			end
		end
	elseif Value.Value == 5 then
		for i, v in pairs(Body:GetDescendants()) do
			if v.ClassName == "MeshPart" then
				v.TextureID = Scheme5
			end
		end
	elseif Value.Value == 6 then
		for i, v in pairs(Body:GetDescendants()) do
			if v.ClassName == "MeshPart" then
				v.TextureID = Scheme6
			end
		end
	elseif Value.Value == 7 then
		for i, v in pairs(Body:GetDescendants()) do
			if v.ClassName == "MeshPart" then
				v.TextureID = Scheme7
			end
		end
	elseif Value.Value == 8 then
		for i, v in pairs(Body:GetDescendants()) do
			if v.ClassName == "MeshPart" then
				v.TextureID = Scheme8
			end
		end
	end
end)

this one is the shortest of them all, but theres other ones that are awfully long and time consuming.
the script works perfectly fine, but the problem is optimizing it, so any help is highly appreciated!
(and if you are wondering, the scheme value is handled by a separate script)

2 Likes

You can use an array to store the Asset id’s and use your value.values to “Search” through the array. tell me if this works or not I made it pretty quick

local Schemes  = {
	
	"rbxassetid://11170714394",
	"rbxassetid://11379831515",
	"rbxassetid://11229241277",
	"rbxassetid://11229244214",
	"rbxassetid://11379843052",
	"rbxassetid://11379861502",
	"rbxassetid://11379862505",
	"rbxassetid://11379863968",
	"rbxassetid://11229247573",
	
}


local Car = game.Workspace.Cars:WaitForChild(Plr.. "Car")
local Body = Car:FindFirstChild("Body")

local Value = script.Parent.SchemeValue
script.Parent.SchemeValue.Changed:Connect(function()
	for i, v in pairs(Body:GetDescendants()) do
		if v.ClassName == "MeshPart" then
			if Schemes[(Value.Value + 1)] then
				v.TextureID = Schemes[(Value.Value + 1)]
			end
		end
	end
	
end)

Edit: Forgot value.value can equal 0 so I added a +1

oh wow, it works like a charm! thank you so much

No problem! I also make another quick edit for another +1 in the if statement

for some reason it works without adding it, but thanks anyway :slight_smile:

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