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 'Damage'?

Hello, I made a script and it does not work. Gives an error message Players.OsipStar4ik.Backpack.StartPickaxe.ToolScript1:24: attempt to index nil with 'Damage' I don't know how to fix it. I hope you help! Thanks

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local collect = game:GetService("CollectionService")
local Players = game:GetService("Players")

local tool = script.Parent
local Remotes = ReplicatedStorage:FindFirstChild("Remotes")

local player = Players.LocalPlayer
local character = player.Character
local humanoid = character:FindFirstChild("Humanoid")
local PickaxeSwingAnim = Instance.new("Animation")
PickaxeSwingAnim.AnimationId = "rbxassetid://11458130668"
local animationTrack1 = humanoid:LoadAnimation(PickaxeSwingAnim)

local ToolConfig = require(ReplicatedStorage:FindFirstChild("Config"):FindFirstChild("ToolConfig"))
local OreConfig = require(ReplicatedStorage:FindFirstChild("Config"):FindFirstChild("OreConfig"))


local inventory = player:FindFirstChild("inventory")
local pickaxeName2 = tool.Name
print("Found", player.Name, "Pickaxe Equipped Name!")
local pickaxe = player.Character:FindFirstChild(pickaxeName2)
print (pickaxeName2)
local pickaxeDmg = ToolConfig.pickaxeName2.Damage
local Ore = player.leaderstats.Ore

tool.Activated:Connect(function(player: Player)
	if tool.Enabled == true then
		print ("1")
		for tool, toolTable in pairs(ToolConfig) do
			print("2")
			if tool == pickaxeName2 then
				print("3")
				local Lezvie = script.Parent.Handle.Lezvie
				Remotes.ToolActivated:FireServer()
				animationTrack1:Play()
				tool.Enabled = false
				humanoid.WalkSpeed = 0
				wait(0.75)
				tool.Enabled = true
				humanoid.WalkSpeed = 16
				Lezvie.Touched:Connect(function(hit)
					print("4")
					if collect:HasTag(hit, "RockHitbox") then
						print("5")
						Remotes.OreDamaged:FireServer()
						
					end
				end)		
			end
		end
	end
end)


1 answer

✓ Accepted answer

You're indexing it incorrectly. Since you're doing ToolConfig.pickaxeName2.Damage, it's trying to find an index of pickaxeName2, instead of the tool's name.

You need to use square brackets to index the variable.

Change line 24:

local pickaxeDmg = ToolConfig[pickaxeName2].Damage

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