Why is this happening?

im getting an error from this code:

local module = {}

for i,v in workspace.Abilities:GetChildren() do
	module[v.Name] = {}
end
task.wait(0.1)
module.Default.Punches = 0
module.Default.Power = 25
module.JOJO.Punches = 45
module.JOJO.Power = 35

return module

error is ReplicatedStorage.Modules.Dictionaries.Abilities:7: attempt to index nil with ‘Punches’ - Client - Abilities:7

image

You forgot to define module.Default

Make sure you add a
module.Default = {}
after the task.wait()

At the time your for loop over Abilities:GetChildren() ran, the Default part had probably not replicated to the client yet. Things in the Workspace often replicate after client scripts start running, which is why we have things like WaitForChild and the ChildAdded and DescendantAdded events.

Ideally though, you would not both loop over the children of Abilities but then have hard-coded references to what you think should be in there. Make it data driven. You could add value objects or attributes to the abilities parts to store their Punches and Power initial values, and then the script would do your for loop over Abilities:GetChildren, but also set up something like:

game.Workspace:WaitForChild("Abilities").ChildAdded:Connect( child )
    module[child.Name] = {
        Punches = <read attribute or value object here>,
        Power = <read attribute or value object here>,
    }
end)

Attributes are best IMO, and you could make them easily extensible by just reading them all in a loop, and not having to hard code even the names (Power and Punches in this case). If you use ValueObjects, just be aware that you might need another level of waiting for children, so that as new value objects replicate under each ability part, you add them to your lua table “modules”.

Maybe the script is trying to access module.Default.Punches before module.Default has been initialized. Hard to say, I’ll take a guess…

mod
local module = {}

for i, v in workspace.Abilities:GetChildren() do
    module[v.Name] = {}
end

if not module.Default then
    module.Default = {}
end

module.Default.Punches = 0
module.Default.Power = 25

if module.JOJO then
    module.JOJO.Punches = 45
    module.JOJO.Power = 35
else
    warn("ability not found")
end

return module
or
local module = {}

if workspace:FindFirstChild("Abilities") then
    for _, v in ipairs(workspace.Abilities:GetChildren()) do
        module[v.Name] = {}
    end

    if not module.Default then
        module.Default = {}
    end
    if not module.JOJO then
        module.JOJO = {}
    end

    module.Default.Punches = 0
    module.Default.Power = 25
    module.JOJO.Punches = 45
    module.JOJO.Power = 35
else
    warn("ability not found")
end

return module
different approach
workspace:WaitForChild("Abilities")

local module = {}

for _, v in ipairs(workspace.Abilities:GetChildren()) do
	module[v.Name] = {}
end

module.Default = module.Default or { Punches = 0, Power = 25 }
module.JOJO = module.JOJO or { Punches = 45, Power = 35 }

return module