i work for a tower defense game and im sending towers attack function to a modulescript, and then using :Attack to run the function inside of the modulescript
this has for some reason causes problems with how the towers work
i think it has maybe a reason to do with local functions or local values being sent to the modulescript, but it seems to work with that type of stuff, I HAVE NO IDEA WHAT CAUSES THIS.
this is the script, :AddAttackFunction adds the function, and at the bottom, :Attack essentially runs the function
tower:AddAttackFunction(function(target)
local flighttime = tower:GetFlightTime("Flight_Speed", target)
tower:FireClient("shoot", target, flighttime)
tower:ForecastDamage(target, tower:GetStat("Damage"), flighttime)
tower:Wait(flighttime)
tower:Damage(target, tower:GetStat("Damage"))
end)
while true do
while souls.Value <= 0 do
souls.Changed:Wait()
end
local times = tower:Cooldown()
local enemies = tower:WaitForEnemiesInRange(nil, true)
local savedamt = souls.Value
changesouls(-1)
local target = tower:GetTarget(enemies)
tower:Attack(times, target)
end
this is the modulescript, both functions
tower.AttackFunctions = {}
function tower:AddAttackFunction(func, specificname)
if specificname ~= nil then
assert(type(specificname) == "string", "Invalid function name type")
end
local funcname = specificname or tostring(#self.AttackFunctions + 1)
self.AttackFunctions[funcname] = func
return func
end
function tower:Attack(times, target, specific)
if target == true then
target = self:GetTarget(self:GetEnemiesInRange())
if target == nil then
error("Invalid target provided to: " .. self.Object.Name)
end
end
local function runfunc(func)
for x = 1, times or 1 do
self:TrueRun(func, target)
end
end
if specific then
if self.AttackFunctions [specific] == nil then
error(self.Object.Name.." does not have specific function: "..specific..", cannot force attack")
end
runfunc(self.AttackFunctions [specific])
else
for _, func in self.AttackFunctions do
runfunc(func)
end
end
end
the fact is that if i just replace tower:Attack with the lines in the function, like this:
while true do
while souls.Value <= 0 do
souls.Changed:Wait()
end
local times = tower:Cooldown()
local enemies = tower:WaitForEnemiesInRange(nil, true)
local savedamt = souls.Value
changesouls(-1)
local target = tower:GetTarget(enemies)
--below is me moving the attack function in place of :Attack
local flighttime = tower:GetFlightTime("Flight_Speed", target)
tower:FireClient("shoot", target, flighttime)
tower:ForecastDamage(target, tower:GetStat("Damage"), flighttime)
tower:Wait(flighttime)
tower:Damage(target, tower:GetStat("Damage"))
end
IT WORKS JUST FINE, I cannot find out what causes this and I need help, please provide any help if you figure out what causes this, thank you.
there are no errors, and there are just random things that happens that are small but large problems at the same time.
also if you want me to send videos of the differences, i can.