Trying to mimic Mouse.Target for mobile

Greetings! I made a script that mimics Mouse.Target on mobile with the use of TouchTapInWorld, however, I am experiencing an issue.

TouchTapInWorld = UserInputService.TouchTapInWorld:Connect(function(Position, TappedByUI)
			print("Tapped")
			if not TappedByUI then
				print("Its not UI.")
				local WorldRay = Camera:ScreenPointToRay(Position.X, Position.Y)

				local RayParams = RaycastParams.new()

				local Raycast = Workspace:Raycast(WorldRay.Origin, WorldRay.Direction, RayParams)

				print("Raycast successful")
				if Raycast then -- Stops after this
					print("We got a result")
					if Raycast.Instance then
						print("We got an instance")
						if Raycast.Instance.Parent:IsA("Model") or Raycast.Instance:IsA("Part") then
							print("Valid 1")
							if Raycast.Instance:IsDescendantOf(Workspace._Plots[Player.Team.Name].ItemHolder) then
								print("Valid 2")
								if Raycast.Instance.Parent:IsA("Model") then
									ReplicatedStorage.DeleteItem:FireServer(Raycast.Instance.Parent)
								elseif Raycast.Instance:IsA("Part") then
									ReplicatedStorage.DeleteItem:FireServer(Raycast.Instance)
								end
							end
						end
					end
				end
			end
		end)

The last print statement I got was “Raycast successful”
So this means that there isn’t a RaycastResult

Please look over my code and tell me what to change.

I am also fairly new to raycasts as this is my first code that uses Raycasts.

Thanks in advance!
Sincerely, Willi

1 Like

On the line of code that does the raycast, multiply the WorldRay.Direction by some big number like 1000 for the ray to be long and hit things from a long distance

2 Likes

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