UserInputService issue. (Please help)

Hi. So recently, I had a UserInputService script that didn’t work, and another scripter helped me fix it. However, now I am noticing a few bugs.

  1. What do you want to achieve? I want to make the MouseButton1Down work exactly like the UserInputService.TouchTapInWorld function.

  2. What is the issue? The MouseButton1Down function works, but it isn’t registering that the edges of the clicked parts exist.

  3. What solutions have you tried so far? I’ve tried using a GuiInset, which actually made an offset, so that didn’t work.

Alright so, if you can help me make my MouseButton1Down function work exactly like the UserInputService.TouchTapInWorld function, that will help a lot, because on mobile, it registers that the edges of parts exist. Whereas on a PC with a mouse, it doesn’t register the edges of parts. Instead, you have to click directly in the middle completely avoiding edges. (If this is just a hit box issue that ROBLOX has, please let me know so you don’t have to waste your time.)

Here’s the code:
(THIS CODE IS IN A LOCALSCRIPT)

local Tool = script.Parent
local Client = game.Players.LocalPlayer
local TransferEvent = Tool:WaitForChild("Transfering")
local UserInputService = game:GetService("UserInputService")
local Camera = game.Workspace:WaitForChild("Camera")
local COOLDOWN = 2.3
local Cooling = false
local GuiService = game:GetService("GuiService")
local Inset = GuiService:GetGuiInset()
Activated = false

Tool.Equipped:Connect(function()
	Activated = true
end)

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 - Inset.X, iObject.Position.Y - Inset.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
				print(hitPart.Name)
				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

Tool.Unequipped:Connect(function()
	Activated = false
end)

Well, I’m hoping the difference is just because you subtract the gui inset in camera:ViewportPointToRay() call in the MouseButton1Down function but not in the TouchTapInWorld function. But I believe you should be using ScreenPointToRay() instead, Also you should use workspace:Raycast() instead of workspace:GetPartOnRay() apparently.

All in all your code should look something like this:

local Tool = script.Parent
local Client = game.Players.LocalPlayer
local TransferEvent = Tool:WaitForChild("Transfering")
local UserInputService = game:GetService("UserInputService")
local Camera = game.Workspace:WaitForChild("Camera")
local COOLDOWN = 2.3
local Cooling = false
local GuiService = game:GetService("GuiService")
Activated = false

Tool.Equipped:Connect(function()
	Activated = true
end)

if UserInputService.MouseEnabled == true then
	UserInputService.InputBegan:Connect(function(iObject, Processed)
		if iObject.UserInputType == Enum.UserInputType.MouseButton1 then
			local Length = 5000

			local unitRay = Camera:ScreenPointToRay(iObject.Position.X, iObject.Position.Y)
			local firedRay = workspace:Raycast(unitRay.Origin, unitRay.Direction * Length)

			if Activated == true and Cooling == false and TransferEvent ~= nil and firedRay then
				print(firedRay.Instance.Name)
				Cooling = true
				TransferEvent:FireServer(firedRay.Position)
				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 firedRay = workspace:Raycast(unitRay.Origin, unitRay.Direction * Length)

		if Activated == true and Cooling == false and TransferEvent ~= nil and firedRay then
			Cooling = true
			TransferEvent:FireServer(firedRay.Position)
			task.wait(COOLDOWN)
			Cooling = false
		end
	end)
end

Tool.Unequipped:Connect(function()
	Activated = false
end)
1 Like

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