Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Why is my bullet touched event only detecting my player and tool?

game.ReplicatedStorage.RayCast.OnServerEvent:Connect(function(player,exit,mousehit)
	local debounce = false
	local ray = Ray.new(exit.Position, (mousehit.Position - exit.CFrame.Position).unit * 500)
	local part,position = workspace:FindPartOnRay(ray,player.Character,false,false)
	local bullet = Instance.new("Part",workspace)
	bullet.Material = Enum.Material.Neon
	bullet.BrickColor = BrickColor.new("Bright yellow")
	bullet.CanCollide = false
	bullet.Anchored = true
	bullet.Name = "Ak-47 Bullet"
	local distance = (exit.Position - position).magnitude
	bullet.Size = Vector3.new(-8.855, 0.025,distance)
	bullet.CFrame = CFrame.new(exit.Position, position) * CFrame.Angles(math.rad(math.random(0,5)),math.rad(math.random(0,9)),0) * CFrame.new(0,0,-distance / 2)
	bullet.Touched:Connect(function(hit)
		print(hit)
	end)
end)

The Touched event does not detect the dummy or baseplate, it only detects the children of the tool and children of my player. How do i fix this?

1 comment

0

Well, you're telling the ray to find player.Character when you use FindPartOnRay(). You should try to assign parameter 2 to a generic rig and not a locked one like player.Character.

1 answer

Hello there!

I would suggest doing this on line 14.

if part then
	local humanoid = part:FindFirstChild('Humanoid')
	if humanoid then
		print("hit")
	end
end

I don't suggest using .Touched since it's very junky and unreliable. Raycast parts might do a better job at that, it's more reliable.

Happy coding!

This discussion is preserved as it was. New answers, comments, and votes are closed.