Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
-1

attempt to index nil with 'Gold'?

Asked by 2 years ago

I'm making a tds game and i experienced an error.

I tried using :WaitForChild() but it gave me the same exact error.

function towerModule.attackMob(newTower, player)
    local config = newTower.Config
    local target = findNearestTarget(newTower, config.Range.Value)
    if target  and target:FindFirstChild('Zombie') and target.Zombie.Health > 0 then
        local targetCFrame = CFrame.lookAt(newTower.HumanoidRootPart.Position, target.HumanoidRootPart.Position)
        newTower.HumanoidRootPart.BodyGyro.CFrame = targetCFrame

        target.Zombie:TakeDamage(config.Damage.Value)
        if target.Zombie.Health <= 0 then
            player.Gold.Value += target.Zombie.MaxHealth
        end
        task.wait(config.Cooldown.Value)
    end
    task.wait(0.1)

    towerModule.attackMob(newTower)
end

I believe the code block is the reason why it's erroring.

But, here is the error that i am experiencing:

14:56:44.149 ServerScriptService../main../tower:32: attempt to index nil with 'Gold' - Server - ./tower:32 14:56:44.149 Stack Begin - Studio 14:56:44.149 Script 'ServerScriptService../main../tower', Line 32 - function attackMob - Studio - ./tower:32 14:56:44.149 ? Script 'ServerScriptService../main../tower', Line 38 - function attackMob (x49) - Studio - ./tower:38 14:56:44.152 Stack End

I would be very happy for solutions.

0
The line that's causing the error is if target.Zombie.Health <= 0 then player.Gold.Value += target.Zombie.MaxHealth Sj0ppiedep0ppie -5 — 2y
0
Are you sure “Gold” is REALLY inside player? Maybe it’s in the leaderstats. Try player.leaderstats.Gold.Value T3_MasterGamer 2189 — 2y
0
It's in player. Sj0ppiedep0ppie -5 — 2y
0
Ok. T3_MasterGamer 2189 — 2y
View all comments (3 more)
0
In using module.attactMob(), did you put the player in the second argument or left it nil? T3_MasterGamer 2189 — 2y
0
cuz you might've forgot T3_MasterGamer 2189 — 2y
0
I did put the second argument. Sj0ppiedep0ppie -5 — 2y

1 answer

Log in to vote
0
Answered by 2 years ago

Change the line 16 from this:

towerModule.attackMob(newTower)

To this:

towerModule.attackMob(newTower, player)
Ad

Answer this question