How do i go with creating a hitbox that detects any players inside it?

i’m trying to code a hardpoint system for my game, so when a player is inside the hardpoint the hardpoint integrity changes. but when i use GetTouchingParts() for the code, it doesn’t seem to detect the player but only when the hitbox’s CanCollide property is set to true

how do i go with creating a hitbox that detects any players inside it?

    runservice.Heartbeat:Connect(function(dt)
		hardpointtable[v.Name].insurgents = {}
		hardpointtable[v.Name].stf = {}
		local partstouching = v.Hitbox:GetTouchingParts()
		print(partstouching)
		for i, v2 in pairs(partstouching) do
			if v2 == "Terrain" then return end
			local humanoid = v2.Parent:FindFirstChildOfClass("Humanoid")
			print("Pass 3")

(this is only the portion of the code)

Try GetPartBoundsInBox

1 Like

Simple solutions: Touch events, region3, :gettouchingparts(), :getpartboundsinbox()
a bit advanced: raycast with attachments

this is for detecting, now making the hitbox depends

if u want a simple hitbox u can create a new part instance and position it to the lookvector of the hrp, and weld them together, and detect if that part was touched by a player

else if u want a more accurate one you can use raycast

wow, never knew that was a thing
the only thing is that now my performance is butchered :joy:

That’s probably because you run a loop every frame try changing that heartbeat connection with a while loop

replacing the loop with a while loop will only make it so it does the entire function individually instead of all hardpoints operating at a time

To fix that check out spawn and task.spawn and coroutines use them carefully though or it may decrease your performance

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