Problems with Table in module script

hello! im having some trouble with my damage module script. I have a dodge system in place and i’d like to play a random animation from a table whenever the player dodges.

Heres the code im using (Full)

function module.DealDamage(base, ATKmod, char, distance)
	local plr = game.Players:GetPlayerFromCharacter(char)
	local HRP = char.PrimaryPart
	local hum = char.Humanoid
	
	local dodgeAnims = {
		one = RS.Animations.Dodges.One,
		two = RS.Animations.Dodges.Two
	}

	
	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 hitChar and hitHum then
					local magnitude = (HRP.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 SPD = hitBP.Stats.SPD
								local random = math.random(1,10)
								if random <= SPD.Value then
									local d = math.random(1, #dodgeAnims)
									local anim = hum.Animator:LoadAnimation(dodgeAnims[d])
									anim:Play()
									print("Dodged", random)
									return {hitChar}
								end

								
								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 SPD = statsFolder.SPD
								local random = math.random(1,10)
								if random <= SPD.Value then
									local d = math.random(1, #dodgeAnims)
									local anim = hum.Animator:LoadAnimation(dodgeAnims[d])
									anim:Play()
									print("Dodged", random)
									return {hitChar}
								end
								
								local totalDamage = module.TotalDamageCalculationAsync(base, ATKmod, DEF)
								hitHum:TakeDamage(totalDamage)
								return {hitChar, totalDamage}
							end							
						end
					end
				end
			end
		end
	end
end

heres the parts of code thats erroring

	local dodgeAnims = {
		one = RS.Animations.Dodges.One,
		two = RS.Animations.Dodges.Two
	}
--The table is supposedly empty
local d = math.random(1, #dodgeAnims) --The second interval doesnt exist
local anim = hum.Animator:LoadAnimation(dodgeAnims[d])
anim:Play()

The output errors out saying that the table is empty, and im unsure why that is. My thoughts is that tables wont function in a module script, but im not entirely sure what the issue is. Help is greatly appriciated, thank you for your time.

local dodgeAnims = {
	RS.Animations.Dodges.One,
	RS.Animations.Dodges.Two
}

Remove the indexes, and that fixes your problem.

dodgeAnims[1] with your old table would fetch nil, since nothing was there with that index. What you were looking to do, was dodgeTable["one"], but I fixed the issue for you.

2 Likes

I see. This worked! thank you so much!