How do i make it so i can click on an object to with a keybind clickdetector

So i made a inventory with a pickup system and idk how to do it with mouse button 1 click

local UIS = game:GetService("UserInputService")
local pickupkey = "F"
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local PickupItem = ReplicatedStorage:WaitForChild("PickupItem")

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()

local PlayerGui = player:WaitForChild("PlayerGui")
local PickupInfoGui = PlayerGui:WaitForChild("PickupInfoGui")

UIS.InputChanged:Connect(function(input)
	if mouse.Target then
		if mouse.Target:FindFirstChild("Pickable") then
			local item = mouse.target
			
			PickupInfoGui.Adornee = item
			PickupInfoGui.ObjectName.Text = item.name
		else
			PickupInfoGui.Adornee = nil
		
		end
	end
end)
local wood = game.Workspace.Wood
UIS.InputEnded:Connect(function(input)
	if input.KeyCode == Enum.KeyCode[pickupkey] then
		if mouse.Target then
			if mouse.Target:FindFirstChild("Pickable") then
				local item = mouse.Target
				if item then
					local distanceFromItem = player:DistanceFromCharacter(item.Position)
					if distanceFromItem < 30 then
						wood.ClickDetector.MouseClick:Connect(function()
							PickupItem:FireServer(item)
							
						
						end)
					end
				end
			end
		end
	end
end)

I suggest you use the Proximity Prompt.

1 Like