#Attempting to replace my Tower Defense game config files with a modulescript. need help

32 messages · Page 1 of 1 (latest)

wispy forge
#

I was following a tutorial for a tower defense and recently found a better way to store tower and mob stats within a module script rather than individual config files. I am not advanced enough to do this so I need help.

#
function tower.Spawn(player, name, cframe, previous)
    local allowedToSpawn = tower.CheckSpawn(player, name)

    if allowedToSpawn then
        
        local towerspawn
        if previous then
            previous:Destroy()
            towerspawn = RSS.Towers.Upgrades[name]:Clone()
        else
            towerspawn = RSS.Towers[name]:Clone()
            player.placedTowers.Value += 1
        end
        
        local ownerValue = Instance.new('StringValue')
        ownerValue.Name = 'Owner'
        ownerValue.Value = player.Name
        ownerValue.Parent = towerspawn
        
        local towerspawn = RSS.Towers[name]:Clone()
        towerspawn.HumanoidRootPart.CFrame = cframe
        towerspawn.Parent = workspace["Towers Placed"]    


        for i, object in ipairs(towerspawn:GetDescendants()) do
            if object:IsA('BasePart') then
                object.CollisionGroup = "tower"
                tower.Optimize(towerspawn)
            end
        end
        
        player.Gold.Value -= towerStats[towerspawn.Name]['Price']
        
        
        coroutine.wrap(tower.Attack)(towerspawn, player) -- line 179
    else        
        warn('tower doesnt exist:', name)
    end
end
#
function tower.FindTarget(towerspawn)
    local towerData = towerStats[towerspawn.Name]
    local range = towerData.Range -- line 110
    local targetMode = towerData.TargetMode
    local bestTarget = nil
    local bestDistance = nil
    local bestWaypoint = nil
    local bestHealth = nil

    for i, mob in ipairs(workspace.Mobs:GetChildren()) do
        local distanceToMob = (mob.HumanoidRootPart.Position - towerspawn.HumanoidRootPart.Position).Magnitude
        local distanceToWaypoint = (mob.HumanoidRootPart.Position - map.Waypoints[mob.MovingTo.Value].Position).Magnitude

        if distanceToMob <= range then
            if targetMode == "Nearest" then
                if distanceToMob < bestDistance then
                    bestDistance = distanceToMob
                    bestTarget = mob
                elseif targetMode == 'First' then    
                    if not bestWaypoint or mob.MovingTo.Value >= bestWaypoint then
                        bestWaypoint = mob.MovingTo.Value
                        
                        if not bestDistance or distanceToWaypoint < bestDistance then
                            bestDistance = distanceToWaypoint
                            bestTarget = mob
                            
                        end
                    end
                end
            end
        end
    end

    return bestTarget
end```
#
    Noob = {
        Price = 1,
        Damage = 1,
        Cooldown = 0,
        Range = 1000,
        Stealth = true,
        TargetMode = '',
        Upgrade1 = 'Noob100',
        Upgrade2 = 'Noob010',
        Upgrade3 = 'Noob001',
        Special = 'None',
        Picture = 'rbxassetid://16379612331', -- add picture id here
        Attack = '', -- add attackanim id here
        Idle = '' -- add idleanim id here
    }
}

return towerStats
#

ServerScriptService.Main.Tower:178: ServerScriptService.Main.Tower:110: attempt to index nil with 'Range'
my error message

flint radish
#

towerData.Range

#

Is not range

#

towerData.Noob["Range"] i think you meant

wispy forge
flint radish
#

Look into the towerspawn

#

print the name of it

wispy forge
#

oh

#

wait

flint radish
#

What did u get

wispy forge
#

it gave me the table

flint radish
#

Oh lol

#

that shouldnt give you a table

#

it should give you "Noob"

wispy forge
flint radish
#

well thats why it doesnt work

wispy forge
#

maybe i could fix it by just adding a name into the table

flint radish
#

and index that

#

I mean you're aleardy indexing it .Name

#

Just add a Name in the tower and make it be "Noob"

#

["Name"] = "Noob"

wispy forge
#

i think i fixed the other issue

#

getting this now

#

after replacing config files with the modulescript i cant use towerspawn as a model anymore

#

full script

#

i need to fix it so it still takes towerspawn as a model but takes from towerStats whenever it wants to pull a stat