Mobile raycast tap to print position bug

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? I would like to make a line of code that excludes the player as a target of the ray and only the other npcs/players

  2. What is the issue? doesnt work

  3. What solutions have you tried so far? numerous relational operators, if statements, functions, nothing works.

NOTE: this script only works during mobile test mode and the local script is placed in starter character scripts

local UIS = game:GetService("UserInputService")
local camera = workspace.Camera
local filter = {}
UIS.TouchTapInWorld:Connect(function(position, processed)

	local ray = camera:ViewportPointToRay(position.X, position.Y, 0)
	

	ray = Ray.new(ray.Origin, ray.Direction * 500)


	local target, point, surface = workspace:FindPartOnRayWithIgnoreList(ray, filter)
	print(target)
end
1 Like

I mean you are using raycast with ignore list, so that’s a good start, all you have to do is insert the player’s character parts into it.

for I,v in ipairs(game.Players.LocalPlayer.Character:GetChildren()) do
   table.insert(filter, I)
end

Wrote on mobile sorry for typos

just add this

local player = game.PLayers.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()

-- And on the ignore list:
local target, point, surface = workspace:FindPartOnRayWithIgnoreList(character, ray, filter)

“unable to cast array to bool”

This might work

ignore:table.insert(character, ignore)

If it does not work this is where I got it from
which might help even more.

I added an optional feature in the new ignore list that I’ve created for you, in case you use bullets or want selected parts to be ignored
Try this (havent bugtested it yet)

local UIS = game:GetService("UserInputService")
local camera = workspace.Camera
local filter = {}

local UIS = game:GetService("UserInputService")
local camera = workspace.Camera

local SetupIgnoreList = function()
	local List = {}
	local Character = game.Players.LocalPlayer.Character
	for _, v in pairs(Character:GetChildren()) do
		if v:IsA("Accessory") or v:IsA("MeshPart") or v:IsA("Part") then
			table.insert(List, v)
		end
	end
	
	--[[
	(OPTIONAL)
	Create folder, name it 'IgnoredItems', or whatever you want, and put it in workspace
	
	This will make it so that there are multiple parts in workspace that are ignored
	If you do happen to add rays or bullets, then I suggest you put them in this IgnoredItems folder
	
	Code:
	for _, v in pairs(game.Workspace.IgnoredItems:GetChildren()) do
		table.insert(List, v)
	end
	]]--
	
	return List
end

UIS.TouchTapInWorld:Connect(function(position, processed)

	local ray = camera:ViewportPointToRay(position.X, position.Y, 0)


	ray = Ray.new(ray.Origin, ray.Direction * 500)
	
	local IgnoreList = SetupIgnoreList()

	local target, point, surface = workspace:FindPartOnRayWithIgnoreList(ray, IgnoreList)
	print(target)
end

You know, I have a simpler solution for this.
You are currently using a deprecated version of raycasting. Refer to: Raycasting | Documentation - Roblox Creator Hub

but if you would like to stick with the deprecated one, add your character to the filter list.

I made a quick module for this:

local QuickCast = {}
QuickCast.__index = QuickCast

function QuickCast.new(Parameters)
	local self = setmetatable({}, QuickCast)
	
	self.RaycastParameters = Parameters
	
	return self
end

function QuickCast:Cast(Origin, Direction)
	local NewRay = workspace:Raycast(Origin, Direction, self.RaycastParameters)
	
	return NewRay or {
		Instance = nil,
		Position = Origin + Direction,
		Material = Enum.Material.Air,
		Normal = nil
	}
end


return QuickCast

The above uses rayparams, which is for the latest version of raycasting

1 Like