Attempt to index nil with 'Hitbox' (I've been stuck on this for so long, and it's probably so obvious...)

I’m trying to make module script that runs damage and animations based on each weapon you have in your inventory/equipped etc.

But before I can even do anything with that, I’m having issues with simply making the tool even swing with any damage.

I keep getting “10:45:19.989 ServerScriptService.ModuleScript:48: attempt to index nil with ‘Hitbox’ - Server - ModuleScript:48”

It’s always either at line 48 or 49. I’m assuming that means the Hitbox can’t be defined. But I don’t understand why. If you scroll at the bottom I made it so any “Tool” thats in the character’s children is called a “tool”, and when I do “tool.” the script auto shows “Hitbox” so it clearly knows the part I’m talking about.

I even tried other people’s scripts from other devforums that apparently work in completely ordinary scripts and I’m still getting the issue.

I still suck at scripting, maybe I’m missing something super obvious please help!


image

Here’s the whole code incase you need it, but it’s saying

local Hitbox = tool == tool.Hitbox
Hitbox.Touched:Connect(function(Blade)

is the issue

Whole module script:

local module = {}

BaseWeaponDamageValues = {
	SwordDamage = 15,
	SpearDamage = 10,
	WarHammerDamage = 30,
	DaggersDamage = 7.5,
	Claws = 10,
	Mace = 30,
	Chains = 5,
	Fist = 5,
	Fish = 1,
	Bows = 5 -- only one with not same dps as others bc extra range
}

module.Weapon = function()
	game.Players.PlayerAdded:Connect(function(player)
		player.CharacterAdded:Connect(function(character)

			--{{ VARIABLES }}--
			local animationSwing = Instance.new("Animation")
			local animationIdle = Instance.new("Animation")
			animationSwing.AnimationId = "rbxassetid://17007907725"
			animationIdle.AnimationId = "rbxassetid://17006666497"
			local idle
			local debounce = false

			--{{ FUNCTIONS }}--

			-- Swing Animation
			local function performSwing(tool)
				if not debounce then
					debounce = true

					if idle then
						idle:Stop()
					end

					local humanoid = character:FindFirstChildOfClass("Humanoid")
					local animator = humanoid and humanoid:FindFirstChild("Animator")

					if animator then
						local swingAnimation = animator:LoadAnimation(animationSwing)
						swingAnimation:Play()

						local debounce = false
						local Damage = 10 -- You are able to change this to how much damage you want the tool to do each time it hits the player.
						local Hitbox = tool.Hitbox
						Hitbox.Touched:Connect(function(Blade) -- Blade is just whatever the hitbox of whatever weapon
							task.wait(0.1)
							local model = Blade.Parent
							if model and model:FindFirstChildWhichIsA("Humanoid") and not (Blade.Parent:FindFirstChildWhichIsA("Folder") == script.Parent:FindFirstChildWhichIsA("Folder")) then
								if not debounce then
									local humanoid = Blade.Parent:FindFirstChild("Humanoid")
									humanoid:TakeDamge(Damage)
									debounce = true
									task.wait(0.8)
									debounce = false
								end
							end
						end)


						swingAnimation.Ended:Connect(function()

							if idle then
								idle:Play()
							end

							debounce = false
						end)
					end
				end
			end

			-- Idle Animation
			local function startIdle()
				idle = character.Humanoid:LoadAnimation(animationIdle)
				idle.Priority = Enum.AnimationPriority.Action
				idle.Looped = true
				idle:Play()
			end

			local function stopIdle()
				if idle then
					idle:Stop()
				end
			end

			local function onToolAdded(tool)
				tool.Activated:Connect(performSwing)
				tool.Equipped:Connect(startIdle)
				tool.Unequipped:Connect(stopIdle)
			end

			-- Connect ChildAdded event to listen for tools being added to the character
			character.ChildAdded:Connect(function(child)
				if child:IsA("Tool") then
					onToolAdded(child)
				end
			end)

			-- Check existing tools in character's backpack
			for _, tool in ipairs(character:GetChildren()) do
				if tool:IsA("Tool") then
					onToolAdded(tool)
				end
			end
		end)
	end)
end

return module

Don’t mind the actual damage script, I can’t even test it yet seeing the issue at hand.

2 Likes

Wouldn’t it just be Hitbox = tool.Hitbox?

2 Likes

i’m going to contribute to this

image

shouldn’t this be

local Hitbox = tool.Hitbox 

unless you’re using some sort of advanced method that i do not know about

got rid of that tool.Parent because i just saw the explorer screenshot sorry :skull:

1 Like

My bad I was messing with it before I sent it, yeah I had it like that before. Still doesn’t work
image

1 Like

I was messing with it before so thats why it’s different now, but yeah before I changed it still didn’t work.

local Hitbox = tool.Hitbox 

it still doesn’t work some reason

image

1 Like

i don’t see tool defined anywhere

local tool = -- your tool
1 Like

Isn’t that what this is

		for _, tool in ipairs(character:GetChildren()) do
			if tool:IsA("Tool") then
				onToolAdded(tool)
			end
		end

now if you just put “tool” in any function like

		local function onToolAdded(tool)
			tool.Activated:Connect(performSwing)
			tool.Equipped:Connect(startIdle)
			tool.Unequipped:Connect(stopIdle)
		end

it’s “defined” as whichever tool you have. I also used to get that issue, but I fixed it by just putting (tool) in

local function performSwing(tool)

If I’m wrong how would I fix this exactly? Like where would I put the variable tool to fix this?

i don’t know if you’re wrong or not, but putting that tool variable would probably just be at the top of the script or at the top of the function

edit:
though i do think that for loop should be above the hitbox = tool.hitbox

I put the variable in the “module.Weapon = function()” as

module.Weapon = function(tool)

and the put the variable

local Hitbox = tool.Hitbox

Still didn’t work. I guess it makes sense it wouldn’t change anything if the hitbox is already defined before it’s actually needed lol like at the original

local Hitbox = tool.Hitbox

spot

Anyone else know how to fix this issue?

1 Like

The issue you’re facing is due to the fact that tool.Activated does not pass the tool as a parameter:

Your performSwing function (which is outside of the scope that the tool variable is defined):

Since it doesn’t pass itself as a parameter, tool for performSwing is going to be nil. You would have to pass it yourself like so:

-- Passing extra parameters:
tool.Activated:Connect(function()
    performSwing(tool)
end)

-- Unable to pass extra parameters (what you have):
tool.Activated:Connect(performSwing)
2 Likes

THANK YOU BRO :sob::sob::sob::sob::sob::sob:

(need more text to reply bruh)

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.