When I start the Hitbox and then destroy it, I get the following error:
“ReplicatedStorage.Modules.HitboxClass:281: Attempt to compare number <nil”
In this line of the module:
function Hitbox:Start()
if self.Lifetime > 0 then ---- The error occurs on this line
if not self.Timer then
self.Timer = Timer.new(0.1, function()
self.Lifetime -= 0.1
if self.Lifetime <= 0 then
self:Destroy()
end
end)
else
self.Timer:On()
end
end
I have tried using :Stop, to stop the Hitbox, that helped the error stop appearing, but it causes several hitboxes to be created and welded to the player. When I use :Destroy it helped me solve that problem, however, I get this error that causes delays in the game.
I also want to show the code, in which I use the module.:
local tool = script.Parent
local remoteEvent = tool.InputAbility
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local HitboxClass = require(ReplicatedStorage.Modules.HitboxClass)
local HitboxTypes = require(ReplicatedStorage.Modules.HitboxClass.Types)
local PunchDebounce = false
local Equipped = false
local Player = tool.Parent.Parent
local Char = Player.Character or tool.Parent
local PunchHitboxParams = {
SizeOrPart = Vector3.new(2.5,2,3.5),
SpatialOption = "InBox",
Blacklist = {Char},
LookingFor = "Humanoid",
Debug = true,
} :: HitboxTypes.HitboxParams
----------------ANIMATIONS--------------------------
local PunchAnimation = Char.Humanoid.Animator:LoadAnimation(script.Punch)
-------------------------------------------------------------------
remoteEvent.OnServerEvent:Connect(function(plr, Input, Info)
if Equipped == true then
local character = tool.Parent or Char
local player = game.Players:GetPlayerFromCharacter(character)
----------- Punch -------------------------
if Input == Enum.UserInputType.MouseButton1 then
if PunchDebounce == false then
PunchDebounce = true
local PunchHitbox, connected = HitboxClass.new(PunchHitboxParams)
PunchHitbox.WeldTo(PunchHitbox, Char.HumanoidRootPart, CFrame.new(0.25, 0.5, -4.5))
PunchAnimation:GetMarkerReachedSignal("StartTheHitbox"):Connect(function()
PunchHitbox:Start()
end)
PunchAnimation:GetMarkerReachedSignal("StopTheHitbox"):Connect(function()
PunchHitbox:Destroy()
end)
PunchHitbox.HitSomeone:Connect(function(Char)
for _, Model in ipairs(Char) do
print(Char)
------- Stuff --------
end
end)
PunchAnimation:Play()
PunchAnimation:AdjustSpeed(1.5)
PunchAnimation.Stopped:Wait()
task.wait(.5)
PunchDebounce = false
end
end
end
end)
Using Stop():
Several hitboxes are created here so that the player can spot the npc multiple times, because these hitboxes stack.
Using Destroy():
Here the npc is only detected once per hitbox created, but the error appears.
The error is probably in my code, but I don’t know how to solve the problem so that it works correctly.