Module Script Table not a valid member

I am trying to get a cooldown to use as a wait, yes I’m not using task.wait currently, I’ll fix that later, but it keeps popping up the error

DecodedAttack is not a valid member of ModuleScript "ServerScriptService.Attack-Related_Scripts.ClassModules.Peasant"

for [DecodedAttack] in

wait(require(game:GetService("ServerScriptService")["Attack-Related_Scripts"].ClassModules[ClassKey][DecodedAttack][DecodedAttack.."_Cooldown"]))

The original function is below.

Script
local function AttackAnimation(player, ID, Class, Attack)
	local animator = player.Character:FindFirstChild("Humanoid"):FindFirstChild("Animator")
	local Animation = Instance.new("Animation")
	Animation.AnimationId = HttpService:JSONDecode(ID)
	
	local AnimationLoaded = animator:LoadAnimation(Animation)
	AnimationLoaded:Play()
	AnimationLoaded.Priority = 1
	
	local ClassKey = HttpService:JSONDecode(Class)
	local DecodedAttack = HttpService:JSONDecode(Attack)
	print(DecodedAttack)
	
	wait(require(game:GetService("ServerScriptService")["Attack-Related_Scripts"].ClassModules[ClassKey].DecodedAttack[DecodedAttack.."_Cooldown"]))
Module Script
local Peasant = {
	Attack1 = {
		Attack1_AnimationId = "rbxassetid://6550920054", --Attack1 Animation Id
		Attack1_Damage = 10, -- Attack Damage
		Attack1_StatDamage = 0.1,
		Attack1_Cooldown = 1.5
	};
	Attack2 = {
		Attack2_AnimationId = "rbxassetid://7070234299",
		Attack2_Damage = 15,
		Attack2_StatDamage = 0.3,
		Attack2_Cooldown = 5
	};
	Attack3 = { Attack3_AnimationId = "rbxassetid://7071810900",
		Attack3_Damage = 7,
		Attack3_Ticks = 2,
		Attack3_TBT = 0.5, --Time Between Ticks
		Attack3_StatDamage = 0.2,
		Attack3_Cooldown = 4
	}
}
return Peasant

I have tried testing out a few things like using [ClassKey] again since the module script and table of Decoded Attack have the same name and have ClassKey twice for the same reason.

2 Likes

It has to do with how you’re requiring it:

Try this:

wait(require(game:GetService("ServerScriptService")["Attack-Related_Scripts"].ClassModules[ClassKey])[DecodedAttack][DecodedAttack.."_Cooldown"])

I also suggest you try using variables, it’ll clean your code right up:

local classKeyModule = require(game:GetService("ServerScriptService")["Attack-Related_Scripts"].ClassModules[ClassKey])
wait(classKeyModule[DecodedAttack][DecodedAttack.."_Cooldown"])
2 Likes

Oh thanks, I’ll try, and is the parenthesis in

[ClassKey])

supposed to be there?

Yeah, that parenthesis should be there.

1 Like

Thanks, it works now, question though, what was the error from my original line from? I want to know if possible to avoid it again, just the parenthesis?

1 Like

Yeah basically just the location of the parenthesis.

Basically, to break it down, what you were doing is:

local var = game:GetService("ServerScriptService")["Attack-Related_Scripts"].ClassModules[ClassKey][DecodedAttack][DecodedAttack.."_Cooldown"]
var = require(var)
wait(var)

And since DecodedAttack doesn’t exist inside of the ClassModules[ClassKey] module, that’s what was throwing the error.

Thanks, I see what it was now. Thanks again for the help.

1 Like