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?
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.
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)
All 8 parts that were selected as occupied are inserted in list.
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 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.
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.