I made a shield system (Code Feedback Please)

My game has some skills when being activated gives you a shield for a certain time, the maximum shield per player is 3, and each shield blocks the damage of 1 skill, so I made this system, when the player uses a skill that gives shield I call this module:

    local Shield = {}

local module = {}
       function module.New(caract)
	       local Data = {
		       ["Shield"] = 0
	       }
	         Shield[caract] = Data
       end

       function module.Delet(caract)
	      Shield[caract] = nil
       end
        
       function module.Set(caract,value)
	       Shield[caract]["Shield"] = value
       end

       function module.Get(caract)
	       if Shield[caract] == nil then
		       module.New(caract)
	       end
	       return Shield[caract]["Shield"]
       end

       function module.RemoveShield(caract,value,shieldTime, initialValue)
	       for count = 1, shieldTime, 1 do
		       if initialValue ~= module.Get(caract)then
			      break
		       else
			      if module.Get(caract) > 0 then
				      module.Set(caract,module.Get(caract)-value)
			      end 
			   end
		       wait(1)
	       end
       end

       function module.Increase(caract,value, shieldTime)
	       if (module.Get(caract) + value) > 3 then
		       module.Set(caract,3)
		       module.RemoveShield(caract,value,shieldTime,module.Get(caract))
	       else
		       local amount = module.Get(caract) + value
		       module.Set(caract,amount)
		       module.RemoveShield(caract,value,shieldTime)
		   end
       end


return module

So if anyone have any suggestion tell me please

2 Likes

You should considering using OOP (Object Oriented Programming) for this - It’s going to benefit you greatly seeing as you’re trying to do something like that anyways, you’ve also made a typo in one of your method names: module.Delet.

I also think in your shield data you should have “maxShields” specified somewhere, instead of hardcoding it in module.Increase, overall I don’t think It’s too bad, just a little messy at times.

I think I know what you’re trying to do with shieldTime, but that would be confusing to anyone who didn’t actually write the module, I think the module its self should be keeping track of that.

shieldTime is How much time shield Will still