How do I use GetPartsInPart()

How would I use GetPartsInPart() to replace .Touched in this hitbox script. I generally have no idea where to start or how to use it. All help is appreciated!

local Tool = script.Parent
local Handle = Tool:WaitForChild("Handle")
local db = false
local plrs = game:GetService("Players")
local plr = plrs:GetPlayers()[1] or plrs.PlayerAdded:Wait()
local touched = false

script.Parent.HitBoxEvent.OnServerEvent:Connect(function()
	if db then return end
	db = true

	local Hitbox = Instance.new("Part")
	Hitbox.Parent = script.Parent.Parent
	Hitbox.CFrame = script.Parent.Parent.HumanoidRootPart.CFrame * CFrame.new(0, 0, -1)
	Hitbox.Transparency = 1
	Hitbox.Anchored = true
	Hitbox.Size = Vector3.new(7, 7, 5)
	Hitbox.CanCollide = false

	Hitbox.Touched:Connect(function(hit)
		local npc = hit.Parent:FindFirstChild("NPC")
		local humanoid = hit.Parent:FindFirstChild("Humanoid")

		if npc and humanoid and not touched then
			touched = true
			humanoid:TakeDamage(10)

			if humanoid.Health <= 0 then
				local leaderstats = plr:FindFirstChild("leaderstats")
				if leaderstats and leaderstats:FindFirstChild("Coins") then
					leaderstats.Coins.Value = leaderstats.Coins.Value + 10
				end
			end
		end
	end)
	game.Debris:AddItem(Hitbox, 0.1)

	wait(0.2)
	touched = false
	db = false
end)

The reason I’d rather have GetPartInPart() is because I heard it’s more accurate, and this script doesn’t work on mobile.

3 Likes

it is very easy to use just type workspace:GetPartsInPart(hitbox:- the hitbox that you are using)

2 Likes

Well that’s fairly simple, but, where exactly would I put that in the code?

Do I just completely replace Hitbox.Touched:Connect(function(hit) with workspace:GetPartsInPart(Hitbox)?

2 Likes

This would not work. .Touched is an event. :GetPartsInPart is a method. An event fires whenever something happens. A method only fires when you call it.

2 Likes

Ohhhh! Thanks for the info! I’ll be sure to use this, thanks for the help.

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