How would I make this script more simple and faster?

Hello so I am working on a tiny project that edits a room with tweens when a gui button is clicked for the whole server but the issue I got is that the script is too long and takes a while to finish.

each function does something for each part in the model.

Ive tried putting it in one function but when I try it it takes years for it to finsish.

I want to make it do the editing all at once instead of once part at a time.

here is my script.

local TS = game:GetService("TweenService")


game.ReplicatedStorage:WaitForChild("BiomChange").OnServerEvent:Connect(function(plr, Name)
	game.ReplicatedStorage:WaitForChild("Styles"):FindFirstChild(Name).Parent = workspace
	for i, Chrildren in pairs(script.Parent:GetDescendants()) do
		if Chrildren.Name == "Ground" then
			local Part = Chrildren
			local Goal = {}
			Goal.Color = Color3.new(0.054902, 0.486275, 0.054902)

			local Tweeninfo = TweenInfo.new(1)

			task.wait(0.1)
			local partween = TS:Create(Part, Tweeninfo, Goal):Play()
			Part.Material = Enum.Material.Grass
		end
	end
end)
game.ReplicatedStorage:WaitForChild("BiomChange").OnServerEvent:Connect(function(plr, Name)
	for i, Chrildren in pairs(script.Parent:GetDescendants()) do
		if Chrildren.Name == "1" then
			local Part = Chrildren
			local Goal = {}
			Goal.Transparency = 1

			local Tweeninfo = TweenInfo.new(1)

			task.wait(0.1)
			local partween = TS:Create(Part, Tweeninfo, Goal):Play()
		end
	end
end)
game.ReplicatedStorage:WaitForChild("BiomChange").OnServerEvent:Connect(function(plr, Name)
	for i, Chrildren in pairs(script.Parent:GetDescendants()) do
		if Chrildren.Name == "2" then
			local Part = Chrildren
			local Goal = {}
			Goal.Transparency = 1

			local Tweeninfo = TweenInfo.new(1.5)

			task.wait(0.1)
			local partween = TS:Create(Part, Tweeninfo, Goal):Play()
		end
	end
end)
game.ReplicatedStorage:WaitForChild("BiomChange").OnServerEvent:Connect(function(plr, Name)
	for i, Chrildren in pairs(script.Parent:GetDescendants()) do
		if Chrildren:IsA("SelectionBox") then
			local Part = Chrildren
			local Goal = {}
			Goal.Color3 = Color3.new(1, 1, 1)

			local Tweeninfo = TweenInfo.new(1)
			task.wait(0.1)
			local partween = TS:Create(Part, Tweeninfo, Goal):Play()
		end
	end
end)
game.ReplicatedStorage:WaitForChild("BiomChange").OnServerEvent:Connect(function(plr, Name)
	for i, Chrildren in pairs(script.Parent:GetDescendants()) do
		if Chrildren:IsA("SelectionBox") then
			local Part = Chrildren
			local Goal = {}
			Goal.Transparency = 1

			local Tweeninfo = TweenInfo.new(1)
			task.wait(0.1)
			local partween = TS:Create(Part, Tweeninfo, Goal):Play()
		end
	end
end)
1 Like

There are a couple of things you can do to make this code simpler and faster

Apply Tags to the Items named Ground with the the usage of CollectionService, then you can apply Changes to the Instances without Having to look through an entire hierarchy for the items you want. using script.Parent:GetDescendants() is very inefficient, you are telling the code to look through all the children (Depending on how many there is), the loop will become slower to grab what you desire, so Tagging your Instances and Calling them later is good Practice, but if you only have one set of children, use script.Parent:GetChildren()

For Color3, you are using Color3.new() which using numbers from 0 - 1, if you want a Difference Color Space, it is recommended you to the Following, this is so you don’t have to enter a specific Decimal for a Specific color depending on your Knowledge on a certain Color Space, like for example with RGB, in order to convert the Decimal to a RGB values, you would need to Multiply the Value by 255, and if you know, 255 / 255 = 1 so this should work:

print(math.floor(0.054902 * 255)) -- 14
print(math.floor(0.486275 * 255)) -- 124
print(math.floor(0.054902 * 255)) -- 14

Goal.Color = Color3.fromRGB(14, 124, 14) -- much easier and simpler to do!

Within your BiomChange RemoteEvent, it would be good Practice to store the goals outside the loop, you are basically telling the script to create tables for how many Descendants there are, This isn’t a good idea as it will be creating a bunch of tables for something that can be done with one, so you should have it outside the table premade:

BC_Goal = {
    Transparency = 1;
}

It also generally isn’t a good Idea to use the Same function for Different Usages, if you do this and use FireServer(), it will fire all these Events made, which can cause lag, so you can make a type checker or Check if the item has a specific name using the function rather than another Event, this would make it easier, plus more efficient.

As for CollectionService, use the GetTagged Method to get an Array of Instances to look through, But Remember, it requires a Tag name, So make sure you spell the name correctly to get the Correct Instances.

If you arent using TweenService Events, there is no reason for you to have a Variable to them, add it in later if you want something to happen with the Tween.

And to speed up the Process, you can use task.spawn() to have the code not yield the said process, this makes it so it runs on a Different thread rather than the main thread.

for _,v in CollectionService:GetTagged("myTag") do -- Gets Tagged Instances
    task.spawn(function() -- Fires code Immedietley + doesnt yield
        TS:Create(v, TweenInfo.new(1), BC_Goal):Play() -- Plays Tween
    end)
end

And as for the BiomChange, we can do this:

BiomChange.OnServerEvent:Connect(function(plr, Info, Goal ,Name: string, NameToCheck: string, TypeCheck: boolean?)
    for i,v in CollectionService:GetTagged("myTag") do
        if TypeCheck then -- if TypeCheck true
            if v:IsA(NameToCheck) then -- Checks Class
                TweenService:Create(v, Info, Goal):Play() -- Plays Tween
            end
        else -- if false
            if string.match(v.Name, NameToCheck) then -- find Objects similar to Name
                TweenService:Create(v, Info, Goal):Play() -- Plays Tween
            end
        end
    end
end)
2 Likes

Looking at the code, you are repeating way too many lines, your also making un-needed variables which (prolly) is the reason for the lag, other than these, you have a task.wait(.1) which is obv causing a lot of time difference between each tween.

3 Likes

I havent used

Before so how would I make it tag eveything and apply it to the parts?

I did what you said witch made it load much faster it just now has a big spike of lag.

1 Like

You would be using CollectionService:ApplyTag() to apply tags, but make sure you have the Tag name and Instance

CollectionService:ApplyTag(Instance, TagName)

I am still I bit confused is there a roblox doctmation for CollectionService?

Yes.

1 Like

I understand a bit more now but I still can’t seem to fix my issue.

Sorry for the late reply but I also saw that you are using OnServerEvent for the same remote 5 times?

1 Like

If that is correct than this code should fix everything for you…

local tweenService = game:GetService("TweenService")
local replicatedStorage = game:GetService("ReplicatedStorage")

local biomChange = replicatedStorage:WaitForChild("biomChange")

local function tweenChildren(name, goal, tweenInfo, TweenInfo, isA, changeMaterial)
    for i, v in pairs(script.Parent:GetDescendants()) do
		if name and v.Name ~= name then continue end
		if isA and not v:IsA(isA) then continue end
		if changeMaterial then v.Material = Enum.Material.Grass end

		tweenService:Create(v, tweenInfo or TweenInfo.new(1), goal):Play()

		task.wait()
	end
end

biomChange.OnServerEvent:Connect(function(player, Name)
	game.ReplicatedStorage:WaitForChild("Styles"):FindFirstChild(Name).Parent = workspace

	tweenChildren("Ground", {Color = Color3.fromRGB(14, 124, 14)}, nil, nil, nil, true)
	tweenChildren("1", {Transparency = 1})
	tweenChildren("2", {Transparency = 1}, TweenInfo.new(1.5))
	tweenChildren(nil, {Transparency = 1}, nil, "SelectionBox")
	tweenChildren("2", {Color3 = Color3.fromRGB(255, 255, 255)}, nil, "SelectionBox")
end)

This works much faster thanks.
I did have to edit it a bit due to I was getting some errors but now it wont change the material of the chosen parts.

local tweenService = game:GetService("TweenService")
local replicatedStorage = game:GetService("ReplicatedStorage")

local biomChange = replicatedStorage:WaitForChild("BiomChange")


local function tweenChildren(name, goal, tweenInfo, isA, changeMaterial)
	for i, v in pairs(script.Parent:GetDescendants()) do
		if name and v.Name ~= name then continue end
		if isA and not v:IsA(isA) then continue end
		if changeMaterial then v.Material = Enum.Material.Grass end
		
		local TweenTime = TweenInfo.new(1)

		tweenService:Create(v, TweenTime, goal):Play()

		task.wait()
	end
end

biomChange.OnServerEvent:Connect(function(player, Name)
	game.ReplicatedStorage:WaitForChild("Styles"):FindFirstChild(Name).Parent = workspace

	tweenChildren("Ground", {Color = Color3.fromRGB(14, 124, 14)}, nil, nil, nil, true)
	tweenChildren("1", {Transparency = 1})
	tweenChildren("2", {Transparency = 1}, TweenInfo.new(1.5))
	tweenChildren(nil, {Transparency = 1}, nil, "SelectionBox")
	tweenChildren("2", {Color3 = Color3.fromRGB(255, 255, 255)}, nil, "SelectionBox")
end)
1 Like

This belongs to #help-and-feedback:code-review rather than #help-and-feedback:scripting-support

1 Like

Ah! You have an extra nil passed for the first function call.
Your welcome glad I was able to help!

1 Like