Why isnt this part detecting collisions consistently?

so i wanted to create a punch hitbox that gets created everytime someone clicks, which works.

what doesnt work however, is the part itself. it detects collisions VERY inconsistently, and very rarely. i made the part flash red whenever it detected a collision, and it only detects a collision every 1/10 punches.

anything helps!!

script

local enterEvent = game.ReplicatedStorage.Going_In
local punchEvent = game.ReplicatedStorage.Punch


punchEvent.OnServerEvent:Connect(function(player)
	local character = player.Character
	local humanoid = character:FindFirstChild("Humanoid")
	local hrp = character:FindFirstChild("HumanoidRootPart")
	local leaderstats = player:WaitForChild("leaderstats")
	local punches = leaderstats:WaitForChild("Punches")

	local punchey_part = Instance.new("Part")
	punchey_part.CanCollide = false
	punchey_part.Anchored = true
	punchey_part.Size = Vector3.new(4,4,4)
	punchey_part.CFrame = hrp.CFrame
	punchey_part.Parent = character
	punchey_part.Transparency = 0
	
	local function playAnimationFromServer(character, animation)
		local humanoid = character:FindFirstChildOfClass("Humanoid")
		if humanoid then
			-- need to use animation object for server access
			local animator = humanoid:FindFirstChildOfClass("Animator")
			if animator then
				local animationTrack = animator:LoadAnimation(animation)
				animationTrack:Play()
				return animationTrack
			end
		end
	end

	local animation = Instance.new("Animation")
	animation.AnimationId = "rbxassetid://15196133419"
	playAnimationFromServer(character, animation)
	
	punchey_part.Touched:Connect(function(hit)
		punchey_part.Color = Color3.new(1, 0, 0)
		local debounce = true
		if hit.Parent ~= punchey_part.Parent then
			local human = hit.Parent:FindFirstChild("Humanoid")
			local hrp = hit.Parent:FindFirstChild("HumanoidRootPart")
			if human and debounce == true and hit == hrp and humanoid.Health > 0 then
				human.Health -= 10
				punches.Value += 1
				hit.Parent:FindFirstChild("RagdollTrigger").Value = true
				wait(1)
				hit.Parent:FindFirstChild("RagdollTrigger").Value = false
				debounce = false
			end
		end
	end)
	wait(0.1)
	punchey_part:Destroy()
end)

Touched() isn’t very accurate, I suggest you use workspace:GetPartBoundsInBox()

Or could use workspace:GetPartsInPart(), since he already has a hitbox

im looking at the roblox docs, and im not sure how it works.

can you explain how it works?

workspace:GetPartsBoundInBox(position, size, overlap parameters(optional))
in your case it would be

workspace:GetPartsBoundInBox(hrp.Position, Vector3.new(x,y,z))

but since you already have a part you could do

workspace:GetPartsInPart(punchey_part)

They return a table of parts which you can go trough with a for loop.

local overlapParameters = OverlapParams.new()
overlapParameters.BruteForceAllSlow = true -- so it checks EVERY part that is touching the hitbox
local touchingParts = workspace:GetPartsInPart(punchey_part)
for _, hit in pairs(touchingParts) do
        if hit.Parent ~= punchey_part.Parent then
			local human = hit.Parent:FindFirstChild("Humanoid")
			local hrp = hit.Parent:FindFirstChild("HumanoidRootPart")
			if human and debounce == true and hit == hrp and humanoid.Health > 0 then
				human.Health -= 10
				punches.Value += 1
				hit.Parent:FindFirstChild("RagdollTrigger").Value = true
				wait(1)
				hit.Parent:FindFirstChild("RagdollTrigger").Value = false
				debounce = false
			end
		end
        break
end

This is something you can replace it with

im trying to implement it, but it doesnt work, but it does work. im gonna go do some terrible code surgery and read up on these :getpartsinpart() thing later

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