Trying to task.wait inside of a __newindex metamethod throwing error

Hello! I am trying to wait a certain amount of time inside of a metamethod when a certain index is called but its giving me this error, is there anyway around this

image

function ResourceManager.new(Instance)
   local obj = {}
   obj.Proxy = GiveProxy()
   setmetatable(obj, {
      __newindex = function(t,k,v)
         if k == "Health" then
            if v <= 0 then
               for _, mesh : MeshPart in pairs(obj.Proxy.Instance:GetChildren()) do
                  mesh.CanCollide = false
                  task.spawn(function()
                     local tween = TweenService:Create(mesh, TweenInfo.new(1.5, Enum.EasingStyle.Sine), {Transparency = 1})
                     tween:Play()
                  end)
               end
               obj.Respawning = true
               task.spawn(function()
                  task.wait(10)
                  print("bring it back!")
               end)
            else
               print("Health is now : "..v)
            end
         end
         obj.Proxy[k] = v
      end
   })

   obj.Instance = Instance
   obj.Health = 100
   obj.Respawning = false
   obj.WhatCanDamage = {}
   obj.Parent = obj

   -- Self is referring to the proxy
   function obj:Damage(amount)
      -- Get the health from proxy
      local oldHealth = self.Health
      -- Do madth on that existing value
      local newHealth = oldHealth - amount
      -- Set it which wil invoke the metamethod, which then sets the proxy's value!
      self.Parent.Health = newHealth
   end

   return obj
end
1 Like

So the solution was easier than I thought just threw in a task.spawn and it’s working flawlessly! :sunglasses:

2 Likes

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