Hitbox not disappearing

im trying to make a battlegrounds system, so im following a tutorial because no other tutorial is helpful, and when i try to make the hitbox disappear it doesnt, when i tried to destroy the hitbox i got the error “attempt to call missing method ‘_Visualize’ of table”, when i tried deleting the “self.Box = nil” the hitbox disappeared but the damage is still there, so if i m1 at a different position than the dummy and i walk over to the dummy, the hitbox still does damage to the dummy, any help would be greatful

	playAnimation.KeyframeReached:Connect(function(kf)
		if kf == "Hit" then

			task.spawn(function()
				hitbox:Start()
				task.wait(0.1)
				hitbox:Stop()
			end)

			task.wait(0.1)
			char:SetAttribute("Attacking", false)

			if char:GetAttribute("Combo") == MAX_COMBO then
				task.wait(1.5)
			end

			char:SetAttribute("Punching", false)
		end
	end)

	playAnimation:Play()

end)

heres my module

local rs = game:GetService("RunService")

local HB = {}

local hitboxColor = Color3.fromRGB(255,0,0)
local hitboxTransparency = 0.75

HB.__index = HB

function HB.new()
	local newHitbox = setmetatable({},HB)
	newHitbox.Size = Vector3.new(10,10,10)
	newHitbox.CFrame = CFrame.new(0,5,0)
	newHitbox.Offset = CFrame.new(0,0,0)
	newHitbox.Humanoids = {}
	newHitbox.Detect = false
	newHitbox.ResetOnStop = false
	newHitbox.Visualizer = true
	
	newHitbox.Mode = "Humanoid"
	
	newHitbox.TargetIndex = "Enemy"
	
	newHitbox.onTouch = function(value)
	end
	
	newHitbox.Box = nil
	newHitbox.Connection = nil
	
	return newHitbox
end

local getCframe = 
	{
		["Instance"] = function(point)
			return point.CFrame
		end,

		["CFrame"] = function(point)
			return point
		end,
	}


function HB:_Visualize()
	
	if not self.Visualizer then return end
	
	local cframeType = typeof(self.CFrame)
	local realCFrame = getCframe[cframeType](self.CFrame)
	
	if not self.Box then
		self.Box = Instance.new("BoxHandleAdornment")
		self.Box.CFrame = realCFrame * self.Offset
		self.Box.Adornee = workspace.Terrain
		self.Box.Color3 = hitboxColor
		self.Box.Transparency = hitboxTransparency
		self.Box.Size = self.Size
		self.Box.Parent = workspace.Terrain
	else
		self.Box.CFrame = realCFrame * self.Offset
		self.Box.Size = self.Size
	end
	
end

function HB:_Cast()
	local cframeType = typeof(self.CFrame)
	local realCFrame = getCframe[cframeType](self.CFrame)
	
	local touchingParts = workspace:GetPartBoundsInBox(realCFrame * self.Offset, self.Size)
	for i,v in pairs(touchingParts) do
		
		if self.Mode == "Humanoid" then
			local humanoid = v.Parent:FindFirstChildOfClass("Humanoid")

			if humanoid then
				if not table.find(self.Humanoids, humanoid) then
					table.insert(self.Humanoids,humanoid)
					self.onTouch(humanoid)
				end
			end
		elseif self.Mode == "Target" then
			local targetFolder
			
			local findFolder = false
			
			for _, child in pairs(v.Parent:GetChildren()) do
				if child:IsA("Folder") and child.Name == "TargetFolder" then
					targetFolder = child
					findFolder = true
					break
				end
			end
			
			if not findFolder then
				for _,child in pairs(v.Parent.Parent:GetChildren()) do
					if child:IsA("Folder") and child.Name == "TargetFolder" then
						targetFolder = child
						findFolder = true
						break
					end
				end
			end
			
			if targetFolder then
				self.onTouch(v)
			end
		end

	end
end

function HB:_Clear()
	
	if self.Connection then
		self.Connection:Disconnect()
	end
	
	if self.Box then
		self.Box:Destroy()
		self.Box = nil
	end
	
end

function HB:Start()
	
	task.spawn(function()
		self.Connection = rs.Heartbeat:Connect(function()
			self:_Visualize()
			self:_Cast()
		end)
	end)

end

function HB:Stop()
	if self.ResetOnStop then
		self.Humanoids = {}
	end
	self:_Clear()
end

function HB:Destroy()
	self.Humanoids = {}
	self:_Clear()
	setmetatable(self,nil)
end

return HB

please be weary i followed a tutorial, and didnt make the module

6 Likes

Pretty sure the error you got was the Heartbeat event executing the last thread, so it shouldn’t cause any issues because it inside a separate thread itself.

3 Likes

the only reason i mentioned the error was because after enough M1s the game becomes so laggy its literally unplayable

3 Likes

What do you mean by an “M1”?

This text will be blurred

3 Likes

M1, or MouseButton1 is a term in combat games to describe punching by pressing the left mouse button

2 Likes

I don’t really play combat games so excuse me on that. Can you provide the full code so I can see where you handle inputs?

3 Likes
local rp = game:GetService ("ReplicatedStorage")
local remotes = rp:WaitForChild ("Remotes")

local combatRemote = remotes:WaitForChild("Combat")

local ss = game:GetService("ServerStorage")
local modules = ss:WaitForChild("Modules")

local Hitbox = require(modules:WaitForChild("TomatoHitbox"))
local Slow = require(modules:WaitForChild("TomatoSlow"))
local HitService = require(modules:WaitForChild("HitService"))

local animations = rp:WaitForChild("Animations")
local combatAnims = animations:WaitForChild("Combat")

local Players = game:GetService("Players")

local lastPunch = {}

local MAX_COMBO = 4

Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect (function(char)
		char:SetAttribute("Combo", 0)
	end)
end)

local function changeCombo(char)
	local combo = char:GetAttribute("Combo")
	
	local player = game.Players:GetPlayerFromCharacter(char)
	
	if not lastPunch[player] then
		lastPunch[player] = os.clock()
	end
	
	local passedTime = os.clock() - lastPunch[player]
	
	if passedTime > 1.2 then
		char:SetAttribute("Combo", 1)
	else

		if combo < MAX_COMBO then
			char:SetAttribute("Combo", combo + 1)
		else
			char:SetAttribute("Combo", 1)
		end
	end
	
	lastPunch[player] = os.clock()

	if combo < MAX_COMBO then
		char:SetAttribute("Combo", combo + 1)
	else
		char:SetAttribute("Combo", 1)
	end
end


local function getAnimation(char)
	local combo = char:GetAttribute("Combo")

	local anims = combatAnims:GetChildren()

	local currAnim = anims[combo]

	return currAnim
end

combatRemote.OnServerEvent:connect(function(player)
	local char = player.Character	
	local hum = char:WaitForChild("Humanoid")	
	local humRp = char:WaitForChild("HumanoidRootPart")

	local animator = hum:FindFirstChildOfClass("Animator") or Instance.new("Animator", hum)

	local attacking = char:GetAttribute("Attacking")	
	local punching = char:GetAttribute("Punching") 

	if attacking or punching then return end

	char:SetAttribute("Attacking", true)
	char:SetAttribute("Punching", true)

	changeCombo(char)

	local punchAnim = getAnimation(char)
	local playAnimation = animator:LoadAnimation(punchAnim)

	task.spawn(function()
		repeat
			task.wait()
		until playAnimation.Length > 0
		Slow.Stun(hum,playAnimation.Length,8)
	end)

	--Hitbox
	local hitbox = Hitbox.new()
	hitbox.Size = Vector3.new(7,7,7)
	hitbox.CFrame = humRp
	hitbox.Offset = CFrame.new(0,0,-4)
	hitbox.Visualizer = true

	hitbox.onTouch = function(enemyHum)
		if enemyHum ~= hum then

			HitService.hit(enemyHum,3)

		end
	end


	playAnimation.KeyframeReached:Connect(function(kf)
		if kf == "Hit" then

			task.spawn(function()
				hitbox:Start()
				task.wait(0.1)
				hitbox:Stop()
			end)

			task.wait(0.1)
			char:SetAttribute("Attacking", false)

			if char:GetAttribute("Combo") == MAX_COMBO then
				task.wait(1.5)
			end

			char:SetAttribute("Punching", false)
		end
	end)

	playAnimation:Play()

end)
2 Likes

I assume the combatRemote event corresponds to the MB1 input, right?

1 Like

yea, its a remote event in replicatedstorage

1 Like

There’s nothing inherently wrong with your code besides a minor memory leak, can you replace

playAnimation.KeyframeReached:Connect(function(kf)
		if kf == "Hit" then

			task.spawn(function()
				hitbox:Start()
				task.wait(0.1)
				hitbox:Stop()
			end)

			task.wait(0.1)
			char:SetAttribute("Attacking", false)

			if char:GetAttribute("Combo") == MAX_COMBO then
				task.wait(1.5)
			end

			char:SetAttribute("Punching", false)
		end
	end)

	playAnimation:Play()

with

local connection = playAnimation.KeyframeReached:Connect(function(kf)
		if kf == "Hit" then

			task.spawn(function()
				hitbox:Start()
				task.wait(0.1)
				hitbox:Stop()
			end)

			task.wait(0.1)
			char:SetAttribute("Attacking", false)

			if char:GetAttribute("Combo") == MAX_COMBO then
				task.wait(1.5)
			end

			char:SetAttribute("Punching", false)
		end
	end)

	playAnimation:Play()
    playAnimation.Stopped:Once(function()
        connection:Disconnect()
    end)

Let me know if the problem persists.

i was eating sorry, but ill try that

i tried it but the problem is still there

Hello, are you there? I tried it but it didn’t work

Sorry for the late response, I’m sorry but I’m not really sure what’s wrong with the code, I also wouldn’t recommend following tutorials unless you know exactly what they’re doing.

What happens to newHitbox.onTouch after the Hitbox is HB:_Clear()ed?

Does HitService yield for a few seconds then damage the humanoid?

so i tested it randomly, and it just started working for some reason, i guess roblox studio wanted to be difficult?

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