Attempt to index nil with "RangeBuff"

Hello, i have problem again with my Tower Upgrade script. so, I got this issues when I trying to upgrade my Support Tower. this is my upgrade script:

EventFolder:WaitForChild("UpgradeEvent").OnServerEvent:Connect(function(Player, Tower)
	if Player and Tower then
		if TowerModule.Tower[Tower.Name] then
			if Tower.Level.Value < 5 then
				if Player.Money.Value > TowerModule.Tower[Tower.Name][Tower.Level.Value + 1].Cost then
					Player.Money.Value -= TowerModule.Tower[Tower.Name][Tower.Level.Value + 1].Cost
					for i, upgrdvalue in pairs(Tower:GetChildren()) do
						if upgrdvalue:IsA("NumberValue") then
							if TowerModule.Tower[Tower.Name][Tower.Level.Value + 1][upgrdvalue.Name] then
								upgrdvalue.Value = TowerModule.Tower[Tower.Name][Tower.Level.Value + 1][upgrdvalue.Name]
							else
								warn("Failed to update "..upgrdvalue.Name.." Value because this value is missing or not found; nil")
							end
						end
					end
					if Tower:FindFirstChild("Type").Value == "Support" then
						wait(.1)
						Tower.TowerControl.Disabled = true
						wait()
						Tower.TowerControl.Disabled = false
					end
				end
			end
		end
	end
end)

I use ModuleScript to data my tower stats. this is the module script:

local module = {}

module.Tower = {

	["SupportDummy"] = {
		
		[1] = {
			
			["DamageBuff"] = 1;
			["RangeBuff"] = 1.25;
			["FirespeedBuff"] = 1;
			["Range"] = 15;
			["Cost"] = 500;
			["Level"] = 1
			
		};
		
		[2] = {

			["DamageBuff"] = 1;
			["RangeBuff"] = 1.25;
			["FirespeedBuff"] = 1;
			["Range"] = 18;
			["Cost"] = 750;
			["Level"] = 2

		};
		
		[3] = {

			["DamageBuff"] = 1;
			["RangeBuff"] = 1.25;
			["FirespeedBuff"] = 1.25;
			["Range"] = 18;
			["Cost"] = 1500;
			["Level"] = 3
		};
		
		[4] = {

			["DamageBuff"] = 1;
			["RangeBuff"] = 1.5;
			["FirespeedBuff"] = 1.25;
			["Range"] = 20;
			["Cost"] = 2455;
			["Level"] = 4

		};
		
		[5] = {

			["DamageBuff"] = 1;
			["RangeBuff"] = 1.5;
			["FirespeedBuff"] = 1.5;
			["Range"] = 20;
			["Cost"] = 4500;
			["Level"] = 5
			 
		};
	};
}

return module

the issues is: Somehow when I trying to max my Support tower it says the “Range Buff” is nil. even tho on the data/module the RangeBuff is exist. this is picture the error that I get:

image

any Solutions to fix this?
Thanks^^

Where are you trying to get RangeBuff ?

1 Like

on this code line

upgrdvalue.Value = TowerModule.Tower[Tower.Name][Tower.Level.Value + 1][upgrdvalue.Name]

are you sure that you are getting to SupportDummy then RangeBuff in [1], [2], [3], [4] or [5] ?

yea, im pretty sure i getting to supportDummy then RangeBuff on the [1][2][3][4][5] Because it works fine on other tower

Can you show me your other tower script?

the other tower stats data module?

Not that. Can I see the other main script

Show us line 36 in TowerMain script or the whole function that the line is inside.

1 Like

The other towers scripts are same like the SupportDummy main script

This is the TowerMain Script.

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local EventFolder = ReplicatedStorage:WaitForChild("EventFolder")
local TowerFolder = ReplicatedStorage:WaitForChild("TowerFolder")

local ServerScriptService = game:GetService("ServerScriptService")
local ModuleScript = ServerScriptService:WaitForChild("ModuleScript")
local TowerModule = require(ModuleScript:WaitForChild("TowerModule"))

EventFolder:WaitForChild("PlaceEvent").OnServerEvent:Connect(function(Player, TowerPosition, Tower)
	if Player and Tower then
		local TowerClone = TowerFolder:WaitForChild(Tower):Clone()
		TowerClone.Parent = workspace:WaitForChild("Tower")
		for i, upgrdvalue in pairs(TowerClone:GetChildren()) do
			if upgrdvalue:IsA("NumberValue") then
				upgrdvalue.Value = TowerModule.Tower[TowerClone.Name][1][upgrdvalue.Name]
			end
		end
		TowerClone.Owner.Value = Player.Name
		TowerClone.Level.Value = 1
		if TowerClone.PrimaryPart then
			TowerClone:SetPrimaryPartCFrame(CFrame.new(TowerPosition))
		end
		TowerClone:WaitForChild("TowerControl").Disabled = false
		warn(TowerClone.Name.." Placed by "..Player.Name)
	end
end)

EventFolder:WaitForChild("UpgradeEvent").OnServerEvent:Connect(function(Player, Tower)
	if Player and Tower then
		if TowerModule.Tower[Tower.Name] then
			if Tower.Level.Value < 5 then
				if Player.Money.Value > TowerModule.Tower[Tower.Name][Tower.Level.Value + 1].Cost then
					Player.Money.Value -= TowerModule.Tower[Tower.Name][Tower.Level.Value + 1].Cost
					for i, upgrdvalue in pairs(Tower:GetChildren()) do
						if upgrdvalue:IsA("NumberValue") then
							upgrdvalue.Value = TowerModule.Tower[Tower.Name][Tower.Level.Value + 1][upgrdvalue.Name] -- this is line 36
						end
					end
					if Tower:FindFirstChild("Type").Value == "Support" then
						wait(.1)
						Tower.TowerControl.Disabled = true
						wait()
						Tower.TowerControl.Disabled = false
					end
				end
			end
		end
	end
end)

Try to print it and send us what you got

U add an extra 1 to the value when u check for it, which means if ur on the 5th lvl, it wud thro error, because tht ineex doesnt exist.

image

Okay try to print it until [Tower.Name]

I just fixed the issues with removing “level” on the Tower Data Module. so this happen because when the scripts check the tower stats data, the level is updated first before the RangeBuff that makes the scripts check level 6(which is not on the data) instead and makes RangeBuff are nil or not found. so I change the upgrade script to be like this:

EventFolder:WaitForChild("UpgradeEvent").OnServerEvent:Connect(function(Player, Tower)
	if Player and Tower then
		if TowerModule.Tower[Tower.Name] then
			if Tower.Level.Value < 5 then
				if Player.Money.Value > TowerModule.Tower[Tower.Name][Tower.Level.Value + 1].Cost then
					Player.Money.Value -= TowerModule.Tower[Tower.Name][Tower.Level.Value + 1].Cost
					for i, upgrdvalue in pairs(Tower:GetChildren()) do
						if upgrdvalue:IsA("NumberValue") then
							if upgrdvalue.Name ~= "Level" then -- will ignore values named "Level" because "Level" is not on the Tower Data Module
								upgrdvalue.Value = TowerModule.Tower[Tower.Name][Tower.Level.Value + 1][upgrdvalue.Name]
								print(upgrdvalue.Name, " Has been updated to; "..TowerModule.Tower[Tower.Name][Tower.Level.Value + 1][upgrdvalue.Name])
							end
						end
					end
					Tower.Level.Value += 1 -- Instead I will increase the level value after the other stats updated.
					if Tower:FindFirstChild("Type").Value == "Support" then
						wait(.1)
						Tower.TowerControl.Disabled = true
						wait()
						Tower.TowerControl.Disabled = false
					end
				end
			else
				warn("Tower maxxed out.")
			end
		end
	end
end)

Thank you for helping me and giving me ideas to fix it.^^