Any way to make this code waste less memory for big scaled games?

 -- for trailer finding
local GUI = game.ReplicatedStorage.HouseSystem.Trailer
local GUI2 = game.ReplicatedStorage.HouseSystem.City
local GUI3 = game.ReplicatedStorage.HouseSystem.Cabin
local GUI4 = game.ReplicatedStorage.HouseSystem.Mansion
local GUI5 = game.ReplicatedStorage.HouseSystem.Luxury
local bool = false
for _, v in pairs(workspace:GetDescendants()) do
if v:IsA("BasePart") then
v.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") and v.Name == "TrailerTouchedPart"  and bool == false then 
print("works")
-- code starts here
local plr = game.Players:GetPlayerFromCharacter(hit.Parent)

if not plr then
    return
end

if plr:FindFirstChild("PlayerGui") and bool == false and plr.HouseData:WaitForChild("TrailerOwnedSlot").Value ~= v.Parent.Parent.Parent.Name then
bool = true
GUI:Clone().Parent = plr.PlayerGui
plr.PlayerGui.Trailer.PropertyBought.Value = v.Parent.Parent.Parent
wait(5)
bool = false
elseif plr.HouseData:WaitForChild("TrailerOwnedSlot").Value == v.Parent.Parent.Parent.Name and plr.HouseData:WaitForChild("HasClaimed").Value == false and bool == false then
	bool = true
	game.ReplicatedStorage.HouseSystem.ClaimProperty:Clone().Parent = plr.PlayerGui
  	wait(1)
 	plr.PlayerGui.ClaimProperty.Property.Value = v.Parent.Parent.Parent
	wait(5)
	bool = false
	elseif plr.HouseData:WaitForChild("HouseNumber").Value == v.Parent.Parent.Parent.HouseNumber.Value then
		v.CanCollide = true
		wait(1)
		v.CanCollide = false
	elseif plr.HouseData:WaitForChild("HouseNumber").Value ~= v.Parent.Parent.Parent.HouseNumber.Value and plr.HouseData:WaitForChild("TrailerOwnedSlot").Value == v.Parent.Parent.Parent.Name and plr.HouseData:WaitForChild("HasClaimed").Value == true and plr.HouseData:WaitForChild("ClaimedHouse").Value == v.Parent.Parent.Parent.Name and bool == false then
	bool = true
	game.ReplicatedStorage.HouseSystem.NotYourHouse:Clone().Parent = plr.PlayerGui
	wait(5)
	bool = false	
end
-- ends here
wait(3)

elseif hit.Parent:FindFirstChild("Humanoid") and v.Name == "CityTouchedPart"  and bool == false then 
	
print("works2")
-- code starts here
local plr = game.Players:GetPlayerFromCharacter(hit.Parent)

if not plr then
    return
end

if plr:FindFirstChild("PlayerGui") and bool == false and plr.HouseData:WaitForChild("CityOwnedSlot").Value ~= v.Parent.Parent.Parent.Name then
bool = true
GUI2:Clone().Parent = plr.PlayerGui
plr.PlayerGui.City.PropertyBought.Value = v.Parent.Parent.Parent
wait(5)
bool = false
elseif plr.HouseData:WaitForChild("CityOwnedSlot").Value == v.Parent.Parent.Parent.Name and plr.HouseData:WaitForChild("HasClaimed").Value == false and bool == false then
	bool = true
	game.ReplicatedStorage.HouseSystem.ClaimProperty:Clone().Parent = plr.PlayerGui
  	wait(1)
 	plr.PlayerGui.ClaimProperty.Property.Value = v.Parent.Parent.Parent
	wait(5)
	bool = false
	elseif plr.HouseData:WaitForChild("HouseNumber").Value == v.Parent.Parent.Parent.HouseNumber.Value then
		v.CanCollide = true
		wait(1)
		v.CanCollide = false
	elseif plr.HouseData:WaitForChild("HouseNumber").Value ~= v.Parent.Parent.Parent.HouseNumber.Value and plr.HouseData:WaitForChild("CityOwnedSlot").Value == v.Parent.Parent.Parent.Name and plr.HouseData:WaitForChild("HasClaimed").Value == true and plr.HouseData:WaitForChild("ClaimedHouse").Value == v.Parent.Parent.Parent.Name and bool == false then
	bool = true
	game.ReplicatedStorage.HouseSystem.NotYourHouse:Clone().Parent = plr.PlayerGui
	wait(5)
	bool = false	
end
-- ends here
wait(3)
elseif hit.Parent:FindFirstChild("Humanoid") and v.Name == "CabinTouchedPart"  and bool == false then 
	
print("works3")
-- code starts here
local plr = game.Players:GetPlayerFromCharacter(hit.Parent)

if not plr then
    return
end

if plr:FindFirstChild("PlayerGui") and bool == false and plr.HouseData:WaitForChild("CabinOwnedSlot").Value ~= v.Parent.Parent.Parent.Name then
bool = true
GUI3:Clone().Parent = plr.PlayerGui
plr.PlayerGui.Cabin.PropertyBought.Value = v.Parent.Parent.Parent
wait(5)
bool = false
elseif plr.HouseData:WaitForChild("CabinOwnedSlot").Value == v.Parent.Parent.Parent.Name and plr.HouseData:WaitForChild("HasClaimed").Value == false and bool == false then
	bool = true
	game.ReplicatedStorage.HouseSystem.ClaimProperty:Clone().Parent = plr.PlayerGui
  	wait(1)
 	plr.PlayerGui.ClaimProperty.Property.Value = v.Parent.Parent.Parent
	wait(5)
	bool = false
	elseif plr.HouseData:WaitForChild("HouseNumber").Value == v.Parent.Parent.Parent.HouseNumber.Value then
		v.CanCollide = true
		wait(1)
		v.CanCollide = false
	elseif plr.HouseData:WaitForChild("HouseNumber").Value ~= v.Parent.Parent.Parent.HouseNumber.Value and plr.HouseData:WaitForChild("CabinOwnedSlot").Value == v.Parent.Parent.Parent.Name and plr.HouseData:WaitForChild("HasClaimed").Value == true and plr.HouseData:WaitForChild("ClaimedHouse").Value == v.Parent.Parent.Parent.Name and bool == false then
	bool = true
	game.ReplicatedStorage.HouseSystem.NotYourHouse:Clone().Parent = plr.PlayerGui
	wait(5)
	bool = false	
end

elseif hit.Parent:FindFirstChild("Humanoid") and v.Name == "MansionTouchedPart"  and bool == false then 
	
print("works4")
-- code starts here
local plr = game.Players:GetPlayerFromCharacter(hit.Parent)

if not plr then
    return
end

if plr:FindFirstChild("PlayerGui") and bool == false and plr.HouseData:WaitForChild("CabinOwnedSlot").Value ~= v.Parent.Parent.Parent.Name then
bool = true
GUI4:Clone().Parent = plr.PlayerGui
plr.PlayerGui.Mansion.PropertyBought.Value = v.Parent.Parent.Parent
wait(5)
bool = false
elseif plr.HouseData:WaitForChild("MansionOwnedSlot").Value == v.Parent.Parent.Parent.Name and plr.HouseData:WaitForChild("HasClaimed").Value == false and bool == false then
	bool = true
	game.ReplicatedStorage.HouseSystem.ClaimProperty:Clone().Parent = plr.PlayerGui
  	wait(1)
 	plr.PlayerGui.ClaimProperty.Property.Value = v.Parent.Parent.Parent
	wait(5)
	bool = false
	elseif plr.HouseData:WaitForChild("HouseNumber").Value == v.Parent.Parent.Parent.HouseNumber.Value then
		v.CanCollide = true
		wait(1)
		v.CanCollide = false
	elseif plr.HouseData:WaitForChild("HouseNumber").Value ~= v.Parent.Parent.Parent.HouseNumber.Value and plr.HouseData:WaitForChild("MansionOwnedSlot").Value == v.Parent.Parent.Parent.Name and plr.HouseData:WaitForChild("HasClaimed").Value == true and plr.HouseData:WaitForChild("ClaimedHouse").Value == v.Parent.Parent.Parent.Name and bool == false then
	bool = true
	game.ReplicatedStorage.HouseSystem.NotYourHouse:Clone().Parent = plr.PlayerGui
	wait(5)
	bool = false	
end
elseif hit.Parent:FindFirstChild("Humanoid") and v.Name == "LuxuryTouchedPart"  and bool == false then 
	
print("works5")
-- code starts here
local plr = game.Players:GetPlayerFromCharacter(hit.Parent)

if not plr then
    return
end

if plr:FindFirstChild("PlayerGui") and bool == false and plr.HouseData:WaitForChild("CabinOwnedSlot").Value ~= v.Parent.Parent.Parent.Name then
bool = true
GUI5:Clone().Parent = plr.PlayerGui
plr.PlayerGui.Luxury.PropertyBought.Value = v.Parent.Parent.Parent
wait(5)
bool = false
elseif plr.HouseData:WaitForChild("LuxuryOwnedSlot").Value == v.Parent.Parent.Parent.Name and plr.HouseData:WaitForChild("HasClaimed").Value == false and bool == false then
	bool = true
	game.ReplicatedStorage.HouseSystem.ClaimProperty:Clone().Parent = plr.PlayerGui
  	wait(1)
 	plr.PlayerGui.ClaimProperty.Property.Value = v.Parent.Parent.Parent
	wait(5)
	bool = false
	elseif plr.HouseData:WaitForChild("HouseNumber").Value == v.Parent.Parent.Parent.HouseNumber.Value then
		v.CanCollide = true
		wait(1)
		v.CanCollide = false
	elseif plr.HouseData:WaitForChild("HouseNumber").Value ~= v.Parent.Parent.Parent.HouseNumber.Value and plr.HouseData:WaitForChild("LuxuryOwnedSlot").Value == v.Parent.Parent.Parent.Name and plr.HouseData:WaitForChild("HasClaimed").Value == true and plr.HouseData:WaitForChild("ClaimedHouse").Value == v.Parent.Parent.Parent.Name and bool == false then
	bool = true
	game.ReplicatedStorage.HouseSystem.NotYourHouse:Clone().Parent = plr.PlayerGui
	wait(5)
	bool = false	
end
end
end)
end
end

Can you explain what the code is supposed to do? Also it’s a bit hard to read because of the lack of indentation.

So the code is basically supposed to clone a UI when a brick is touched in different situations of different values.

1 Like

ok, I see a lot of elseifs so i’ll show how I would do this.

local situations = {
    situation1 = {
        situation2 = function()
            -- runs when situation1 and situation2 are met
        end,

        situation3 = {
            -- you can add more cases here
        }
    }
}

Now you can easily traverse the dictionary based on the conditions, and it supports having different amounts of conditions for any outcome. I just named them all “situation” as an example, but you should actually give them proper names.

2 Likes

You’re setting up a connection for every single BasePart in the game :I You can easily avoid most of them and only set up connections for the ones that are actually relevant, like so:

for _, v in pairs(workspace:GetDescendants()) do
if v:IsA("BasePart") and v.Name == "TrailerTouchedPart" then
v.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid")  and bool == false then 

You could set up even fewer connections if you instead checked when a player touches a part, if that part is named “TrailerTouchedPart”. That way you only need 1 connection per player instead of 1 connection per trailer. Although this matters a lot less since I’m guessing you’ll have less than 100 trailers and a couple of 1000 parts.

2 Likes

You should localize some things like ReplicatedStorage to create tidier and better code.

local replicatedStorage = game.ReplicatedStorage

Code indentation makes it hard to read as well but up to you to script with good indentation or not.

1 Like