Detect player touches player

Hi. I’m making bomber tag game like minecraft tnt tag.
I want to make if player has bomb and that player touches other player, bomb is passed.
But it doesn’t work.
Please tell me if you think you have a solution.
(“bombertag” is module script function)

bombertag.PickUp()
task.spawn(function()
	while true do
		task.wait(0.1)
		for _, v in pairs(players:GetPlayers()) do
			v.Character:FindFirstChild("HumanoidRootPart").Touched:Connect(function(hit)
				if hit.Parent ~= v.Character and hit.Parent:FindFirstChildOfClass("Humanoid") then
					if v.Character:FindFirstChild("Bomb") and not hit.Parent:FindFirstChild("Bomb") then
						print("Aww")
						bombertag.GiveBomb(hit.Parent)
						bombertag.RemoveBomb(v.Character)
					end
				end
			end)
		end
	end
end)

task.spawn(function()
	local elapsed = 0
	while true do
		for i = 30, 0, -1 do
			elapsed = i
			print(elapsed)
			task.wait(.1)
		end
		for _, v in pairs(players:GetPlayers()) do
			if v.Character and v.Character:FindFirstChild("Bomb") then
				bombertag.Explosion(v.Character)
			end
		end
		task.wait(5)
		bombertag.PickUp()
	end
end)

Question 1) Does the script above print the statement “Aww”?

Question 2) Can you show the bombertag.GiveBomb(hit.Parent) script?

Thank you for your reply.
Script sometimes doesn’t work. it doesn’t print “Aww” when failed to bass bomb.
This is GiveBomb scrpt.

function bombertag.GiveBomb(char)
	local bomb = Instance.new("Part")
	bomb.Name = "Bomb"
	bomb.Size = Vector3.new(2, 2, 2)
	bomb.CanCollide = false
	bomb.Anchored = false
	bomb.Parent = char
	bomb.CFrame = char.Head.CFrame
	bomb.CFrame *= CFrame.Angles(math.rad(-90), math.rad(0), math.rad(0))

	local mesh = Instance.new("SpecialMesh")
	mesh.MeshId = "rbxasset://fonts/timebomb.mesh"
	mesh.TextureId = "rbxasset://textures/bombtex.png"
	mesh.Parent = bomb

	local weld = Instance.new("WeldConstraint")
	weld.Part0 = char.Head
	weld.Part1 = bomb
	weld.Parent = bomb

	local highlight = Instance.new("Highlight")
	highlight.DepthMode = Enum.HighlightDepthMode.Occluded
	highlight.FillColor = Color3.fromRGB(255, 0, 0)
	highlight.FillTransparency = 0.6
	highlight.OutlineColor = Color3.fromRGB(255, 0, 0)
	highlight.OutlineTransparency = 0
	highlight.Parent = char
end

Use workspace:GetPartsInPart() to detect if the player is touching another player’s character parts, then use the data to pass the bomb to another player.

I think it works with both CanCollide and non-CanCollide objects as it just detects if parts are touching or inside of eachother. Another function does the same, but I’m pretty sure it isn’t as reliable as GetPartsInPart.

bomb.CFrame = char.Head.CFrame * CFrame.Angles(-math.pi/2, 0, 0)

Thank you for reply.
I got weird error and it doesn’t work…
I’m sorry if I wrong :GetPartsInPart() usage.

Thank you for reply.
This method is simple and easier than before. Thank you!

“Argument 1 missing or nil”
You didn’t include any player parts in Argument 1. You didn’t pass anything to the function, which is why it didn’t work.

I watched how to use :GetPartsInPart() after reply, I moved pick up function and want to define player as pickedplr, but an error like the following screenshot occurs.
I don’t understand why such a message appears even though there is an else.

local pickedplr
task.wait(5)

local function PickUp()
	pickedplr = players:GetPlayers()[math.random(1, #players:GetPlayers())]
	bombertag.GiveBomb(pickedplr.Character)
	return pickedplr
end

PickUp()
task.spawn(function()
	while true do
		task.wait(0.1)
		for _, v in pairs(workspace:GetPartsInPart(pickedplr.Character.Torso)) do
			if pickedplr:FindFirstChildOfClass("Humanoid") then
				if v.Parent ~= pickedplr and v.Parent:FindFirstChildOfClass("Humanoid") then
					if pickedplr:FindFirstChild("Bomb") and not v.Parent:FindFirstChild("Bomb") then
						print("Aww")
						bombertag.GiveBomb(v.Parent)
						bombertag.RemoveBomb(pickedplr)
						pickedplr = v.Parent
					end
				end
			
			else
				if v.Parent ~= pickedplr.Character and v.Parent:FindFirstChildOfClass("Humanoid") then
					if pickedplr.Character:FindFirstChild("Bomb") and not v.Parent:FindFirstChild("Bomb") then
						print("Aww")
						bombertag.GiveBomb(v.Parent)
						bombertag.RemoveBomb(pickedplr.Character)
						pickedplr = v.Parent
					end
				end
			
			end
		end
	end
end)

Pretty bad to constantly making a Instance and setting it up everytime someone touches it, it’s best to actually just pass the Object to the other player inventory.

I solved it with for in pairs and touched event! Thank you to everyone who came here!

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