Coding a mobile-friendly throwing knife?

I was wondering what to do, as I am trying to make a knife similar to MM2 from scratch and I want to make it mobile friendly. I’m using ContextActionServices to create a mobile button, and because you have to tap the button, I can’t just use the mouse’s position/target because it will use where you tapped the button as the mouse position. This is where I’m stuck, because I don’t know what to do next. This is my code:

-- This is a local script.
local tool = script.Parent
local handle = tool.Handle
local contextActionService = game:GetService("ContextActionService")
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()

local function throw()
	print("T H R O W")
	local marker = workspace.Marker:Clone()
	marker.CFrame = mouse.Hit
	marker.Parent = workspace
	print(mouse.Hit)
end

tool.Equipped:Connect(function()
	contextActionService:BindAction("throwKnife",throw,true,Enum.KeyCode.E,Enum.KeyCode.ButtonX)
	contextActionService:SetTitle("throwKnife","Throw Knife")
	contextActionService:SetPosition("throwKnife",UDim2.new(0.5,-25,0.5,-25))
end)

tool.Unequipped:Connect(function()
	contextActionService:UnbindAction("throwKnife")
end)

1 Like

I would check out the UserInputService | Roblox Creator Documentation Event. This fires only for taps that do not interact with GUI elements.

1 Like