So I’m trying to make some kind of system to handle multiple moving objects at once, but It got me wondering if I was doing this right, or if it would cause any issues.
The System I have uses CollectionService to Move the Objects with a specified duration, and RunService to Consantly Loop the Objects, which in code looks like this:
self.Runner = RunService.Stepped:Connect(function(time, deltaTime) -- RunService
for index, object in CollectionService:GetTagged(Tag) do -- Gets all Instances from Tag
update(object, deltaTime) -- updates Instances using DeltaTime
end
end)
This would be the update function where the Objects are Updated:
local function update(i: Instance, dt)
-- Attributes to use to apply on Moving Objects
local CF = i:GetAttribute("OldCFrame")
local dur = i:GetAttribute("Duration")
local move = i:GetAttribute("Vector")
local lerp = i:GetAttribute("Lerp")
i:SetAttribute("Lerp", math.min(1, lerp + (dt/dur)))
-- Adds to Lerp (deltaTime / duration)
i.CFrame = if i:GetAttribute("IsLocal") then
CF:Lerp(CF * CFrame.new(move), i:GetAttribute("Lerp"))
else
CF:Lerp(CFrame.new(move) * CF, i:GetAttribute("Lerp"))
-- Applys CFrame
if i:GetAttribute("Lerp") == 1 then -- If Lerp is finished
CollectionService:RemoveTag(i, Tag) -- Remove Object from Tag
end
end
As far as I’m concerned, It works as Intended, but as stated previously, I was wondering about the problems with the code, and whay may occur when using it, one of the main concerns I have was with the constant usage of :GetAttribute() and :SetAttribute(), because this is running every 1/60 of a Second (Based on FPS), would that cause any issues with how it its handled?
Correct me if I’m wrong, but using :GetAttribute() and :SetAttribute() are the only way to interact with attributes? At-least as far as I have learned.
Well, there are the functions: :GetAttribute(), :GetAttributes(), and :SetAttribute()
:GetAttribute() will get you the Data from an Attribute if it exists :GetAttributes() will give you an Array of Attributes from a Instance :SetAttribute() will Apply Data to the Attribute, if it does not exist then it will create it.
But I do not recommend assigning a name to the index in for loops if you’re not using it. This can cause memory leaks, therefore, a bad practice. Many programmers commit this mistake, and I see many professionals being aware of it.
Instead, leave it an underscore, in other words, a dash (_); used in arguments that are not to be used, therefore, a good practice.
I’m not really sure if it affects any server performance. But, you can probably check the script’s information when it is running by using some useful tools such as Script Performance, or you can try to create another system of indexing and setting your custom properties and compare their performance by calculating their speed(?). Just my two cents though.
So I tried both, for the Speed (or benchmarking), I ended up getting these numbers:
0.00002080004196614027 -- Pretty small Number
-- what I did:
local s = os.clock()
update(v, dt)
local e = os.clock() - s
print(e)
-- Let me know if I made a Mistake
And using Script Performance, it doesnt show up when using it in a Separate Script, Is that a good thing?
Well, I can’t really think of any other ways to help you from here now, but you can try to study the script performance tab and check if there are any weird spiking numbers, it really depends on what your script is supposed to be doing so if the Activity or Rate column spikes up high numbers, ensure that your script is not doing any unnecessary works, because you wouldn’t want a normal simple script to have weird big numbers in the script performance which signifies a bigger underlying problem do you?