Is it bad to use :GetAttribute() / :SetAttribute() like this?

Hi,

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.

So yeah, you’re not wrong.

So then, I don’t really think there is any other way to do it, unless someone more knowledgeable is able to correct me.

I see that everything here is fine.

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.

1 Like

Can you please stop saying this? I got it, but I still use it for other things.

Naming an unused variable doesn’t cause a memory leak. It’s just bad practice to name a variable that isn’t used

3 Likes

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.

1 Like

What do you mean by other things? I mean like only leave it an underscore IF you’re not using it, I don’t mean like never use index.

You’re shifting away from the OP’s main issue. So unless you have ways and suggestions to help the main issue, you can leave this conversation.

3 Likes

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?

I mean that’s some good sign.

Can you show me the script performance tab? Run the game, and check the script in script performance, you should see some of its details.

Well, I’m not sure if I did this correctly since I dont really use the Script Performance Tab, but It doesnt seem to show anything happening:

I made sure to check it when it was moving and not moving while some of it was running, and the Scripts showed up, but nothing appeared on them.

On developer Console however, it shows 0.049 on activity
So I may just be paranoid about it.

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?

1 Like

Also, its useful to check out the developer hub for troubleshooting purposes which can help you narrow down the best solution.

Ok, well thank you for helping!

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