Hi Guys, so basically I was working on a simple punching script but it has one issue.
The issue is that the hitbox’s CanTouch property is true but it still can’t register any touch. When I set CanCollide to true aswell, it then registers touch. This is a LocalScript btw. Any help is appreciated, thanks!
local player = game.Players.LocalPlayer
local char = script.Parent
local humanoid = char.Humanoid
local animator = Instance.new("Animator", humanoid)
local canPunch = true
local cas = game:GetService("ContextActionService")
local animations = game.ReplicatedStorage.Animations
local punchingAnimation = animations.Punching
function punch()
if canPunch then
canPunch = false
local hitbox = Instance.new("Part", workspace)
hitbox.Size = Vector3.new(6,6,6)
hitbox.CFrame = char.HumanoidRootPart.CFrame * CFrame.new(0,0,-3.5)
hitbox.Anchored = true
hitbox.Transparency = 0.5
hitbox.CanCollide = false
hitbox.CanTouch = true
animator:LoadAnimation(punchingAnimation):Play()
local touchingParts = hitbox:GetTouchingParts()
for _, part in pairs(touchingParts) do
if part.Parent:FindFirstChild("Humanoid") and part.Parent ~= char then
part.Parent.Humanoid:TakeDamage(10)
break
end
end
wait(3)
canPunch = true
end
end
cas:BindAction("Punch", punch, false, Enum.UserInputType.MouseButton1)
When you put Cancollide to false, the part would just phase through the player and baseplate and fall into the void. Moreover, the :GetTouchingParts() gets an array of things that the part physically hit. Since CanCollide is false, the part would not register a touch since it cannot even be hit. A better alternative to this would be hitbox:GetPartsInPart() as that function returns an array of parts that are in the body of the part.
CanTouch is a newer property the old function which is GetTouchingParts so the function is not updated to suit it. The property that matters is the TouchInterest created when .Touched event is created on a part.
Hence follow @MysteryX2076 suggestion of using the newer GetPartsInPart function. Although this old function does use the Roblox’s collision system so there might be some niche use cases probably not for hitboxes.
Hi there just wanted to clarify myself, a part does not register a touch when it’s cancollide is false because there was no collision detected between the hit instance and the part right?
According to the documentation cancollide does not matter due to the “regardless”. An example of this is the default roblox sword which is can collide false and uses .Touched event.