SetAttribute() does not work

I was trying to make an A* pathfinding algorithm, and for that I was planning to use attributes, however I noticed just right now that the function that is supposed to set attributes, does not work at all. The script is something like this:

for i, n in pairs(Nodes) do
				if math.abs(n.Position.X / 8) == math.abs(center.Position.X / 8) and math.abs(n.Position.Z / 8) == math.abs(center.Position.Z / 8) + 1 then
					UpNeighbor = n
					if UpNeighbor then
						if table.find(OpenList, UpNeighbor) then
							regUpCost = center:GetAttribute("regCost") + 1
							if regUpCost < UpNeighbor:GetAttribute("regCost") then
								UpNeighbor:SetAttribute("regCost", regUpCost)
								UpNeighbor:SetAttribute("CameFrom", center.CFrame)
							else
								UpNeighbor:SetAttribute("regCost", UpNeighbor:GetAttribute("regCost"))
							end
						else
							regUpCost = math.abs((n.Position.X / 8) - (start.Position.X / 8)) + math.abs((n.Position.Z / 8) - (start.Position.Z / 8))
							UpCost = math.abs((finish.Position.X / 8) - (UpNeighbor.Position.X / 8)) + math.abs((finish.Position.Z / 8) - (UpNeighbor.Position.Z) / 8) + regUpCost
							table.insert(OpenList, UpNeighbor)
							table.insert(Costs, UpCost)
						end
					end

This is just the portion of code, with the SetAttribute() used. Checking part attributes, they stay the same all the time being 0. Why could possibly be happening?

1 Like

Why not use value instances? They behave very similarly to attributes.

1 Like

Im afraid im unfamiliar with them…

1 Like

How does this help? No need if it doesn’t help.

1 Like

All of them can store a specific type of value that can be access via .Value.

1 Like

But if attributes can store what he wants to store, I don’t see how these instances would be better.

Yeah, but if attributes aren’t working for him then a good alternative is instances.

1 Like

If the attributes aren’t working, I doubt the instances will.

Two things to make sure of first:

  1. Is the desired code being called on. If you were to stick a print statement before :SetAttribute(), would it print?
  2. Is the value being fed to :SetAttribute() non-zero (also checkable with print statements)?

I am pretty sure that If I were to use print, it would print out a zero. As I stated before, the values don’t change. But I am also pretty sure that the code is being called for, as the local for neighbor is created and further used in the rest of code that I did not show.

because value instances are old and unoptimized, and its cleaner to use attributes

Furthermore, It seems that this issue appears to happen in the part of code that I have shown specifically. Here is another piece that shows that the :GetAttribute() is working.

script.Parent.MouseClick:Connect(function()
	for i, node in pairs(Nodes) do
		if node:GetAttribute("Occupied") == true then
			table.insert(ClosedList, node)
		end
	end
	wait(1)
	print(ClosedList)
end)

image
All 8 parts that were selected as occupied are inserted in list.

Ofc instances are old, They are literally as old as Roblox itself

no i meant specifically value instances, attributes are better because theyre newer mainly because its optimized and its more organized too. the only downside to attributes is it doesn’t support adding objects to it unlike object values

I’m happy to hear that you are pretty sure that it would, now please do so. It doesn’t hurt to know exactly what’s going on.

Ok, I guess that would not hurt indeed.

I think I have just realized what was the problem… I set the wrong type of value for attribute it seems. I will try to change that and see if everything works. Also, the print didn’t work because of that, and it gave out an error, so now we for sure know that code is being called.

Good to hear, always nice when print statements make it especially easy to find out what was wrong with variables.

Yeah, the only use value instances actually have is being able to store references to objects, which I can’t find in the attribute type list. I will say, I have never actually tried setting an attribute to an object via code, does it work? Guess I will just have to test it myself and find out.

It does not. The closest you can have is a CFrame of a part.