Hitbox not working when touched

  1. What do you want to achieve? Keep it simple and clear!

I want to make a hitbox that I have inside the player’s character model be touched by another player.

  1. What is the issue? Include screenshots / videos if possible!

When the other player touches the hitbox of the other character, it doesn’t run the following code.

This is what my hitbox looks like:

  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?

I searched Youtube & the Dev Forum for help but couldn’t find what I needed.

Here is my code. Just for testing purposes I added a print statement after

-- This is an example Lua code block
game.Players.PlayerAdded:Connect(function(plr)
	plr.CharacterAdded:Connect(function(Chr)
		
		
		for a,obj in pairs(Chr:GetChildren()) do
			if obj.Name == "Hitbox" then
				
				obj.Touched:Connect(function(hit)
					if hit.Parent:FindFirstChild("Humanoid") then
						local player = game.Players:GetPlayerFromCharacter(hit.Parent)
						print(obj.Parent.Name.." was touched by "..player.Name)
					end
				end)
			end
		end
	end)
end)

Instead of the detecting if the hitbox touched the character, make it detect if the character touched the hitbox. Hope this helps :slightly_smiling_face:

1 Like

Thanks for the suggestion, I’ll try.

Didn’t work. Wouldn’t I want the hitbox to be the touch object in my case?

The reason the hitbox isn’t registering is that you’re running a for loop looking for it right when the character is created, which does not necessarily mean that the hitbox has even been created yet when the script is looking for it. In general, the for loop is rather inefficient here; you can easily wait for the hitbox you’re looking for to exist by using :WaitForChild() (since I assume there is only one hitbox judging by the image).

game.Players.PlayerAdded:Connect(function(plr)
	plr.CharacterAdded:Connect(function(Chr)
		
		local obj = Chr:WaitForChild("Hitbox")
				
		obj.Touched:Connect(function(hit)
			if hit.Parent:FindFirstChild("Humanoid") then
				local player = game.Players:GetPlayerFromCharacter(hit.Parent)
				print(obj.Parent.Name.." was touched by "..player.Name)
			end
		end)
	end)
end)
1 Like
--//Services
local Players = game:GetService("Players")

--//Functions
Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		local Hitbox = character:WaitForChild("Hitbox")
		
		Hitbox.Touched:Connect(function(hit)
			local Player = Players:GetPlayerFromCharacter(hit.Parent)
			
			if Player then
				print(Hitbox.Name, "was touched by", Player.Name)
			end
		end)
	end)
end)

I tried this and even after I added the WaitForChild it didn’t work.

Do you actually add the hitbox part into the character?

Yes. I add the hitbox when the player joins. Would you like to see that script?

Sure, you can just send the entire script.

1 Like

Here is the script:

--Create hitbox when player joins game

local createCharacterHitbox = function(char)
	local torso = char:WaitForChild("HumanoidRootPart")
	local newPart = Instance.new("Part", char)
	newPart.Size = Vector3.new(4.6, 8, 3) -- Change this for your NPCs.
	newPart.Massless = true
	newPart.CanCollide = false
	newPart.Anchored = false
	newPart.Name = 'Hitbox'
	newPart.CanTouch = true
	newPart.Transparency = 1

	local weld = Instance.new("Weld", torso)
	weld.C0 = CFrame.new(0, 0, 0)
	weld.C1 = CFrame.new()
	weld.Part0 = torso
	weld.Part1 = newPart
	
end

game.Players.PlayerAdded:Connect(function(plr)
	plr.CharacterAdded:Connect(function(Chr)
		createCharacterHitbox(Chr)
	end)
end)

Have you considered you’re possibly not touching another Player and rather a random part or an NPC (won’t get registered by your :GetPlayerFromCharacter() call.)? If so, then that would explain why it’s not printing out anything. Try simply doing “print(hit)” and I’m willing to bet you’ll find out it works just fine.

Yeah when I say print(hit) it just prints out everything in the game that the hitbox touches. But when a player touches it it doesn’t work.