Hello! im making a damage function for my combat module, and i ran into some trouble with returning specific items.
I am trying to achieve a function that will run good with projectiles, but like i said i ran into some issues
Here is the function
----------------------------------
--This part of the code is the relevant part
------------
--Run Damage Function
if hitPlr then
local hitBP = hitPlr.Backpack
local DEFStat = hitBP.Stats.DEF.Value
local totalDamage = module.TotalDamageCalculationAsync(base, ATKmod, DEFStat)
hitHum:TakeDamage(totalDamage)
return {hitChar, totalDamage, ended}
else
local statsFolder = hitChar.Stats
local ATK = statsFolder.ATK.Value
local DEF = statsFolder.DEF.Value
local totalDamage = module.TotalDamageCalculationAsync(base, ATKmod, DEF)
hitHum:TakeDamage(totalDamage)
return {hitChar, totalDamage, ended}
end
heres the part where i call the function
coroutine.wrap(function()
while throwing == true do
print("throw!!!!")
local damage = CS.ProjectileDamage(15, statFolder.ATK.Value, projectile, 7, char)
if damage[3] == "Ended" then
throwing = false
projectile:Destroy()
end
task.wait()
end
end)()
function module.ProjectileDamage(base, ATKmod ,part: BasePart, distance: number, char: Model)
local HRP = char.HumanoidRootPart
local hum = char.Humanoid
local plr = game.Players:GetPlayerFromCharacter(char)
--------------
for i, v in pairs(workspace:GetChildren()) do
if v:IsA("Model") then
local hitChar = v
if hitChar:FindFirstChild("Humanoid") then
local hitPlr = game.Players:GetPlayerFromCharacter(hitChar)
local selectedChar = hitChar:FindFirstChild("Character Selected")
local hitHum = hitChar.Humanoid
local hitHRP = hitChar.PrimaryPart
if hitHum and hitHRP then
local magnitude = (part.Position - hitHRP.Position).Magnitude
if magnitude <= distance then
if hitChar ~= char and hum.Health > 0 then
------------
--Run Damage Function
if hitPlr then
local hitBP = hitPlr.Backpack
local DEFStat = hitBP.Stats.DEF.Value
local totalDamage = module.TotalDamageCalculationAsync(base, ATKmod, DEFStat)
hitHum:TakeDamage(totalDamage)
return {hitChar, totalDamage}
else
local statsFolder = hitChar.Stats
local ATK = statsFolder.ATK.Value
local DEF = statsFolder.DEF.Value
local totalDamage = module.TotalDamageCalculationAsync(base, ATKmod, DEF)
hitHum:TakeDamage(totalDamage)
return {hitChar, totalDamage}
end
end
end
end
end
end
end
end
i also tried printing the table, but it prints out nil. I have a similar function to this one but its not meant for projectiles, and it works perfectly. So i am unsure what i am doing wrong
My only guess is that when your script has not detected any target nearby, it returns nil. Try adding a return {} at the end of the function. Also, both of the return statements you have does not contain any "Ended" string.
Not sure what else you want your script to do other than that. I can’t really see any problem with the code other than those two.
Adding a return {} at the end of the function would guarantee that your other scripts will see it returns something. The other way is to just check in your coroutine whether or not CS.ProjectileDamage returned anything, like so:
if typeof(damage) == "table" then -- if damage is a table. typeof(nil) should return "nil" as string instead. comparing "nil" to "table" equals false.
-- other code here
end
-- Example typeof usage
local int = 4
local str = "Hello World!"
local vector = Vector3.zero
local function foo()
end
for _, item in { int, str, vector, foo } do
print(typeof(item)) -- Prints 'number', 'string', 'function', etc
end