Need help with destroying a region3

i have a problem where even after a part that represents the GetPartsinPart() function gets deleted the region3 still acts as if it is there and if the player walks into that region3 it still damages them which i do not want. i tried unrefrencing it but i am not quite sure how to actually do that.

local RunS = game:GetService("RunService")
local RS = game:GetService("ReplicatedStorage")
local TS = game:GetService("TweenService")
local DS = game:GetService("Debris")
--//Services
local db = false

local AttackModule = {}

function AttackModule.GenerateHitbox(Part,Offset)
	local Hitbox =  Instance.new("Part",Part)
	Hitbox.Size = Vector3.new(5,5,5)
	Hitbox.CFrame = Part.CFrame:ToWorldSpace(CFrame.new(0, 0, -Offset))
	Hitbox.Anchored = true
	Hitbox.CanCollide = false
	local PartsInHitbox = workspace:GetPartsInPart(Hitbox)
	
	return PartsInHitbox,Hitbox  
end

function AttackModule.AttackPlayerInHitbox(Hitbox,Damage) --//Loops through the parts inside the hit box and if it finds a player damage their humanoid
	local ActualPartsinHitbox = workspace:GetPartsInPart(Hitbox)
	local TimesHit = 0
	for i,v in pairs(ActualPartsinHitbox) do
		local player = game.Players:FindFirstChild(v.Parent.Name) 
		local hum = v.Parent:FindFirstChildOfClass("Humanoid")
		if player and hum and db == false then
			db = true
			DS:AddItem(Hitbox,0)
			hum:TakeDamage(Damage)
			ActualPartsinHitbox = nil
			print(hum.Health)
			return
		end
	end
	
end

function AttackModule.GenerateBullet(StartPosition,Direction,Model) --//create a bullet instance and move it and when it hits a player damage them
	local Params = RaycastParams.new()
	Params.FilterType = Enum.RaycastFilterType.Blacklist
	Params.FilterDescendantsInstances = {Model}
	local Raycast = workspace:Raycast(StartPosition,Direction.Unit * 100000,Params)
	--//RayCast
	local PartsinHitbox,Hitbox = AttackModule.GenerateHitbox(Model.HumanoidRootPart,0)
	local magDistance = (StartPosition - Raycast.Position).Magnitude
	local Time = magDistance / 50--Equation is t = s/v
	--//Data
	local Info = TweenInfo.new(Time,Enum.EasingStyle.Linear)
	local Goals = {Position = Raycast.Position}
	local Tween = TS:Create(Hitbox,Info,Goals)
	--//Tween
	Tween:Play()
	
	Tween.Completed:Connect(function()
		DS:AddItem(Hitbox,0)
		db = false
	end)
	
	RunS.Heartbeat:Connect(function()
		AttackModule.AttackPlayerInHitbox(Hitbox,10)
	end)
end

return AttackModule

Could look something like this:

	local connection = nil
	connection = RunS.Heartbeat:Connect(function()
		AttackModule.AttackPlayerInHitbox(Hitbox,10)
	end)

	--When you're done
	connection:Disconnect()

oh wow that worked really well tysm!

1 Like

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