GetAttribute not working

it shows this error code:

ServerScriptService.Main.Tower:82: attempt to index nil with 'GetAttribute'

But I don’t get it?

function tower.Ability(newTower, target)
	local Ability = newTower:GetAttribute("Ability")

	if Ability and Ability == "AOE" then
		local Primary = newTower.PrimaryPart
		local Range = newTower:GetAttribute("AOERange")

		local targets = GetNearestTargets(target.PrimaryPart, Range, workspace.Mobs)

		if targets and #targets >= 1 then
			for k, enemy in pairs(targets) do
				enemy.Humanoid.Health -= newTower.Config.Damage.Value
			end
		end
	end
end

As you can see I’m trying to make it so that my Tower has an AOE Attack Type, how do I fix this?

newTower is likely nil
Run this and see what the outcome is:

function tower.Ability(newTower, target)
    if newTower == nil then warn("[!] newTower returned nil") return end
	local Ability = newTower:GetAttribute("Ability")

	if Ability and Ability == "AOE" then
		local Primary = newTower.PrimaryPart
		local Range = newTower:GetAttribute("AOERange")

		local targets = GetNearestTargets(target.PrimaryPart, Range, workspace.Mobs)

		if targets and #targets >= 1 then
			for k, enemy in pairs(targets) do
				enemy.Humanoid.Health -= newTower.Config.Damage.Value
			end
		end
	end
end

I’ve tried doing it twice and there seems to not be a warning

Was there any errors when you ran it this time?

Is this line 82:

Or is this line 82?

Well, there is one.

ServerScriptService.Main.Tower:207: ServerScriptService.Main.Tower:111: ServerScriptService.Main.Tower:71: attempt to index nil with 'Position' 

But I don’t see anything wrong with this line of code

coroutine.wrap(tower.Attack)(newTower, player)

Line 82 is the first script if you don’t know

Coroutines don’t show exactly where errors happen inside them, so they appear to be the coroutine line that errored. (use task.spawn instead)

1 Like

says it cant call non function thread

Like this:

task.spawn(tower.Attack, newTower, player)

If that doesn’t work, use this:

task.spawn(function()
	tower.Attack(newTower, player)
end

Well, ya fixed the attack, but the AOE still isn’t working now

How is newTower defined outside of the coroutine? It’s probably nil like people said.

1 Like

It was in a module inside a function:

function tower.Spawn(player, name, cframe, previous)
	local allowedToSpawn = tower.CheckSpawn(player, name, previous)
	
	if allowedToSpawn then
		
		local newTower
		local oldMode = nil
		if previous then
			oldMode = previous.Config.TargetMode.Value 
			previous:Destroy()
			newTower = ReplicatedStorage.Towers.Upgrades[name]:Clone()

Ye as @bluebxrrybot said, we need to see calling convention to this code.

And you’re sure that this variable is actually accessible at when you use tower.Ability?
Also have you tried printing newTower at all?

It doesn’t show any checks on the existence of the tower or the function call.

Seems like I need to do some fixing myself, thanks for helping me

Where is the SetAttribute() code or the calling convention to the code in the original post?

Also, any idea why I can’t fix AnimationWeightedBlendFix???

Yea, roblox removed that property on November 1st, as well as forcing it on. I still have to reperfect my animation system because of this. But let’s not talk about that.

1 Like