So Currently, I am trying to make some optimizations on a Tycoon Game on a Group I help work on, and one of the Optimizations I made, makes it so that the Settings now rely on Attributes
instead of ValueBase
Instances
, which after making this optimization, reduced the Memory usage by a whole gigbyte.
But After testing a bit more, I noticed that the Code wasn’t working as intended, and instead just made the game unplayable, when I checked to see if I made any mistakes, by cross-refrencing both old, and new code, as well as old, and new Settings, I cant find the issue.
The Original Settings had a
Folder
calledbuttonSettings
located in the Buttons, which was in charge of keeping track of Settings relatedInstances
, after the Change toAttributes
, they are now located on the Button Model, so all you have to do is call:GetAttribute()
and you have you Data.
This is the original code for adding Player Data, and then looking for the Next Button to Activate, there are no Issues with it on the Original code, the main concern is which the checkNextButton
function, which will be shown later:
tycoon.Buildings.ChildAdded:Connect(function(newObject)
local plr = tycoon.Properties.Owner.Value
if plr then
local profile = dataManager:Get(plr)
if profile.tycoonData[newObject.Name] == nil then
if newObject:FindFirstChild("Lifetime") then
profile.tycoonData[newObject.Name] = {true,true}
else
profile.tycoonData[newObject.Name] = {true,false}
end
else
-- this is the important bit, the above is for gamepass stuff
for index,buttons in tycoon.Buttons:GetChildren() do
-- Iterates through Buttons
if buttons.buttonSettings.buildingObject.Value == newObject.Name then
-- Checks to see if the Values are the same
buttonClose(buttons,true)
-- Removes Button
checkNextButton(tycoon,buttons.buttonSettings.buildingObject.Value)
-- Checks for Next Button
break
end
end
end
end
end)
Here is the Modified code to work with Attributes
, despite looking nearly the exact same, they for some reason function completely different. All the Data was kept the Exact same.
tycoon.Buildings.ChildAdded:Connect(function(newObject)
local plr = tycoon.Properties.Owner.Value
if plr then
local profile = dataManager:Get(plr)
if profile.tycoonData[newObject.Name] == nil then
if newObject:FindFirstChild("Lifetime") then
profile.tycoonData[newObject.Name] = {true,true}
else
profile.tycoonData[newObject.Name] = {true,false}
end
else
-- Again, Important Bit below
for _,b in tycoon.Buttons:GetChildren() do
if b:GetAttribute("buildingObject") == newObject.Name then
buttonClose(b,true)
checkNextButton(tycoon,b:GetAttribute("buildingObject"))
break
end
end
end
end
end)
When Checking for the Next Button, the function will basically look for an Attribute
called Dependancy
, which is the object that Activates the Button, from there it will check if the Object exists in the Buildings
Folder, and if it does not, it will spawn in the button.
Original Code:
local function checkNextButton(tycoon,objectName)
for index,buttons in tycoon.Buttons:GetChildren() do
if buttons:FindFirstChild("buttonSettings"):FindFirstChild("Dependancy") and buttons:FindFirstChild("buttonSettings").Dependancy.Value == objectName then
local object = tycoon.Buildings:FindFirstChild(buttons.buttonSettings.buildingObject.Value)
print(object, object == nil)
if object == nil then
buttonShow(buttons)
end
end
end
end
Modified Code:
local function checkNextButton(tycoon, objectName)
for _,b in tycoon.Buttons:GetChildren() do
local Dependency = b:GetAttribute("Dependancy")
if Dependency and Dependency == objectName then
local object = tycoon.Buildings:FindFirstChild(objectName)
print(object, object == nil)
if object == nil then
buttonShow(b)
end
end
end
end
By looking at these two blocks of code, there is still almost no difference between the two, they should both have the exact same result when used, but when debugging, the Attribute
System for some reason detects the condition (object == nil
) completely false, while the ValueBase
System does not, for a test, I stepped on a button, reset the game, and then pick a tycoon, and these were the results it gave me after printing the Instance
, and Condition;
Original Code Output:
-- object | object == nil (boolean)
nil true
nil true
-- The Two objects do not appear here after purchased
Modified Code Output:
-- object | object == nil (boolean)
Cnv1 false
Cnv1 false
-- It now Decides to print the name of the instances existing
-- when before they didnt
As a result, the Buttons do not appear, making the game unplayable.
What could be the cause of this issue?