UserInputService Issue

Hi there. I’m having an UserInputService issue. Basically, the player is supposed to click on a part and a model will spawn there in that position if the part exists. However, althought the model is spawning in the correct position, an offset is being set too, and I have no idea why that is. (This code is in a LocalScript, and the ServerScript works fine, but if you need the ServerScript code, I can send that as well.)

  1. What do you want to achieve? Model spawns on the part and not in the middle of the sky.

  2. What is the issue? The model spawns over the part, like an offset is being set or something.

  3. What solutions have you tried so far? Tried changing the code around, nothing happened. Same result.

If you can help me with this issue, please do. I’m very confused on what to do, especially since I don’t know much about UserInputService nor Raycasting. Here is the code:

if UserInputService.MouseEnabled == true then
	UserInputService.InputBegan:Connect(function(iObject, Processed)
		if iObject.UserInputType == Enum.UserInputType.MouseButton1 then
			local Length = 5000
			
			local unitRay = Camera:ViewportPointToRay(iObject.Position.X, iObject.Position.Y)
			local RayBeam = Ray.new(unitRay.Origin, unitRay.Direction * Length)
			local hitPart, HitPos = workspace:FindPartOnRay(RayBeam)
			
			if Activated == true and Cooling == false and TransferEvent ~= nil and hitPart then
				Cooling = true
				TransferEvent:FireServer(HitPos)
				task.wait(COOLDOWN)
				Cooling = false
			end
		end
	end)
		
elseif UserInputService.TouchEnabled == true then
	UserInputService.TouchTapInWorld:Connect(function(Position, processedByUI)
		local Length = 5000
		if processedByUI then
			return
		end
		
		local unitRay = Camera:ViewportPointToRay(Position.X, Position.Y)
     	local RayBeam = Ray.new(unitRay.Origin, unitRay.Direction * Length)
     	local hitPart, HitPos = workspace:FindPartOnRay(RayBeam)
		
		if Activated == true and Cooling == false and TransferEvent ~= nil and hitPart then
			Cooling = true
			TransferEvent:FireServer(HitPos)
			task.wait(COOLDOWN)
			Cooling = false
		end
	end)
end

This is probably because of the GUI inset. Try adding this to your code:

-- put this at the top
local GuiService = game:GetService("GuiService")

-- where you are dealing with the UserInputObject, subtract the GUI inset
local inset = GuiService:GetGuiInset()
local unitRay = Camera:ViewportPointToRay(iObject.Position.X - inset.X, iObject.Position.Y - inset.Y)

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