Function returns nil

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)()

here is the output:

^^i used a remote function if that is relevant

help would greatly be appreciated, thank you for your time

1 Like

Have you printed what damage returns? (e.g) print(damage) -- prints the entire table.

nevermind that.

What’s the entire function (starting from function CS.ProjectileDamage... to its end)?

The code above seems correct, but it appears your CS.ProjectileDamage function is incomplete, so it must be on a different section of your Script?

1 Like

Hello! so sorry for the long wait!

heres the entire function

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 :sweat_smile:

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.

1 Like

i see. Also i removed the “ended” cause i thought something would change lol

but what would happen if i return {}? it would be an empty table and i am unsure how that would help

This seems like the issue! I can see what i can do to combat this

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
1 Like

This helped! thank you so much!

I have an idea of what typeof does but can i get a description (or maybe provide an API link if possible)

Links:

https://create.roblox.com/docs/scripting/luau#types

-- 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
1 Like