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!
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.