UserInputService bug

  1. What do you want to achieve? Working code.

  2. What is the issue? Code isn’t working.

  3. What solutions have you tried so far? Actually, I got it from the developer hub, but when I tried to make it work to see how I could possibly tweak it, uh, it’s not working.

Hi there. Basically, that entire survey of questions is all you need to know. The script is parented inside the workspace, not sure if that helps or not. Here’s the code:

local UserInputService = game:GetService("UserInputService")

local camera = workspace.CurrentCamera
local LENGTH = 500

local function createPart(position, processedByUI)
	-- Do not create a part if the player clicked on a GUI/UI element
	if processedByUI then
		return
	end

	-- Get Vector3 world position from the Vector2 viewport position
	local unitRay = camera:ViewportPointToRay(position.X, position.Y)
	local ray = Ray.new(unitRay.Origin, unitRay.Direction * LENGTH)
	local hitPart, worldPosition = workspace:FindPartOnRay(ray)

	-- Create a new part at the world position if the player clicked on a part
	-- Do not create a new part if player clicks on empty skybox
	if hitPart then
		local part = Instance.new("Part")
		part.Parent = workspace
		part.Anchored = true
		part.Size = Vector3.new(1, 1, 1)
		part.Position = worldPosition
	end
end

UserInputService.TouchTapInWorld:Connect(createPart)

this isn’t a bug. your script is parented to workspace which is expected to serve everyone, you should make this script as a localscript and put it in starterplayerscripts. (userinputservice and workspace.CurrentCamera will work on client-side.)

2 Likes

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