Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

attempt to index nil with 'Gold'?

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.

7 comments

0

The line that's causing the error is if target.Zombie.Health <= 0 then player.Gold.Value += target.Zombie.MaxHealth

0

Are you sure “Gold” is REALLY inside player? Maybe it’s in the leaderstats. Try player.leaderstats.Gold.Value

0

It's in player.

0

Ok.

0

In using module.attactMob(), did you put the player in the second argument or left it nil?

0

cuz you might've forgot

0

I did put the second argument.

1 answer

✓ Accepted answer

Change the line 16 from this:

towerModule.attackMob(newTower)

To this:

towerModule.attackMob(newTower, player)

This discussion is preserved as it was. New answers, comments, and votes are closed.