kater3ds
(katerCodes)
February 9, 2024, 3:40pm
#1
I am trying to detect if a value inside a table changes. What I tried was this but it didn’t work lol.
enemyTable.Health.Changed:Connect(function()
newFolderForEnemy:SetAttribute("health", enemyTable.Health)
end)
enemyTable looks something like this:
local enemyTable = {
Health = 100,
},
Is there any easy way of doing so?
1 Like
You could use __newindex, but that requires a metatable however
Metatables attach powerful metamethods to tables, allowing for manipulation like indexing, addition, and concatenation.
kater3ds
(katerCodes)
February 9, 2024, 3:48pm
#3
How could something like this look? An example would be nice :>
Hi! Try to do this idk:
local enemyTable = {
Health = Instance.new("NumberValue")
}
enemyTable.Health.Value = 100
enemyTable.Health.Changed:Connect(function(new)
newFolderForEnemy:SetAttribute("health", new)
end)
kater3ds
(katerCodes)
February 9, 2024, 3:49pm
#5
Yes that’s a way but my system sadly requires it to be a value inside a table. No instance
hmm, try to do this:
local enemyTable = {Health = {Old = 100, New = 100}}
game["Run Service"].RenderStepped:Connect(function()
if enemyTable.Health.Old ~= enemyTable.Health.New then
enemyTable.Health.Old = enemyTable.Health.New
newFolderForEnemy:SetAttribute("health", enemyTable.Health.New
)
end
end)
wait(1)
enemyTable.Health.New = 90
I think something like this
local Enemy = {}
local EnemyTable = {
Health = 100,
__newindex = function(self, index, value)
print("Table changed: " .. index .. " = " .. tostring(value)) -- just a test
rawset(self, index, value)
end
}
local Monster = setmetatable(Enemy, EnemyTable)
Monster.Health = 5 -- just a test
2 Likes
system
(system)
Closed
February 23, 2024, 3:54pm
#8
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.