I made a HitBox with region3 (I haven't added any type of cooldown/debounce) that was supposed to lower a part's health after a hit. But what happens is that after I active my tool and the 'activate' value in my tool is turned to true, the while/for i, v loop stops work. These are the scripts for both my region3 and my tool.
Region3 script: local RegionPart = game.Workspace.RegionPart
local pos1 = RegionPart.Position - (RegionPart.Size / 2)
local pos2 = RegionPart.Position + (RegionPart.Size / 2)
local Region = Region3.new(pos1, pos2)
while true do
print("Started/Restarted Loop!")
wait()
local PartsInRegion = game.Workspace:FindPartsInRegion3(Region, RegionPart, 1000)
for i, RegionPart in pairs(PartsInRegion) do
if RegionPart.Parent:FindFirstChild("Humanoid") ~= nil then
print("Found the Humanoid!")
if RegionPart.Parent:FindFirstChild("Sword") ~= nil then
print("Found the Sword!")
if RegionPart.Parent.Sword:FindFirstChild("Handle") ~= nil then
print("Found the Handle!")
if RegionPart.Parent.Sword.Handle.Active.Value == true then
print("Decreasing RegionPart's Health!")
RegionPart.Humanoid.Health = RegionPart.Humanoid.Health - 30
end
end
end
end
end
end
Tool Script:
local Tool = script.Parent.Parent
local Active = Tool.Handle.Active
Tool.Activated:Connect(function()
Active.Value = true
end)
Tool.Deactivated:Connect(function()
Active.Value = false
end)
Explosion script for my part:
while true do
wait()
if script.Parent.Humanoid.Health == 0 then
local Explosion = Instance.new("Explosion")
Explosion.BlastRadius = 200
Explosion.Position = script.Parent.Position
end
end
Please format with Code block aka.
This
For easier reading!
The problem
I think what's going on in your region script is your calling RegionPart.Parent
on objects you find in your region3. The problem with this is when objects get removed inside the area they are still referenced in code, but their parent is nil aka deleted. To get round this problem i would change the following code:
local RegionPart = game.Workspace.RegionPart local pos1 = RegionPart.Position - (RegionPart.Size / 2) local pos2 = RegionPart.Position + (RegionPart.Size / 2) local Region = Region3.new(pos1, pos2) while true do print("Started/Restarted Loop!") wait() local PartsInRegion = game.Workspace:FindPartsInRegion3(Region, RegionPart, 1000) for i, RegionPart in pairs(PartsInRegion) do if RegionPart.Parent:FindFirstChild("Humanoid") ~= nil then print("Found the Humanoid!") if RegionPart.Parent:FindFirstChild("Sword") ~= nil then print("Found the Sword!") if RegionPart.Parent.Sword:FindFirstChild("Handle") ~= nil then print("Found the Handle!") if RegionPart.Parent.Sword.Handle.Active.Value == true then print("Decreasing RegionPart's Health!") RegionPart.Humanoid.Health = RegionPart.Humanoid.Health - 30 end end end end end end
To:
local RegionPart = game.Workspace.RegionPart local pos1 = RegionPart.Position - (RegionPart.Size / 2) local pos2 = RegionPart.Position + (RegionPart.Size / 2) local Region = Region3.new(pos1, pos2) while true do print("Started/Restarted Loop!") wait() local PartsInRegion = game.Workspace:FindPartsInRegion3(Region, RegionPart, 1000) for i, RegionPart in pairs(PartsInRegion) do if(RegionPart.Parent ~= nil) then if RegionPart.Parent:FindFirstChild("Humanoid") ~= nil then print("Found the Humanoid!") if RegionPart.Parent:FindFirstChild("Sword") ~= nil then print("Found the Sword!") if RegionPart.Parent.Sword:FindFirstChild("Handle") ~= nil then print("Found the Handle!") if RegionPart.Parent.Sword.Handle.Active.Value == true then print("Decreasing RegionPart's Health!") local HumanoidChk = RegionPart:FindFirstChild("Humanoid") --// check to see if this part has a humanoid if so lets damage it! if(HumanoidChk ~= nil) then RegionPart.Humanoid.Health = RegionPart.Humanoid.Health - 30 --// this would be better and simpler to do the following --[[ Un comment this section after removing the above code as both do the same thing. The only difference between is your way will damage humanoid regardless if the humanoid has a forcefield or not but the way below allows for immunities with the ForceField aka doesn't damage the humanoid! HumanoidChk:TakeDamage(30) ]] end end end end end end end end
The rest of the code/scripts look fine!
Reference: https://developer.roblox.com/en-us/api-reference/function/Humanoid/TakeDamage
Hope this helps!