Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Script runs when mouse button 1 or 2 is clicked?

Id like to make it so it only works when left click (button 1) is clicked, thanks in advance!

local enabled = true
local ding = game.Workspace.Sounds.Ding				-- sound
local part = game.Workspace.Configuration.DesertScout1	-- name
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local UserInputService = game:GetService("UserInputService")
local PlayerGui = player:WaitForChild("PlayerGui")
local toolup = PlayerGui:WaitForChild("clickE")
local maxpickup = 20
local gui = PlayerGui.HatUI.DesertScoutAdd



	UserInputService.InputBegan:Connect(function(input,gameProccesedEvent)
	
		local item = mouse.Target
		local distance = player:DistanceFromCharacter(item.Position)
if mouse.Target == part and enabled == true and distance <= maxpickup and mouse.Button1Down then
		ding:Play()
		gui.Enabled = true
		part.Position = Vector3.new(100,100,100)
		enabled = false
		
		part.Transparency = 1
		part.CanCollide = false
	
		game.ReplicatedStorage.HatEvent.DesertScoutEvent:FireServer()
		
wait(1)
		gui.Enabled = false
		enabled = true
		part.Transparency = 0
		part.CanCollide = true
		
	
end
	end)

2 answers

Mouse.Button1Down isn't used here because of how UIS works. Instead, we look for the UserInputType MouseButton1


local enabled = true
local ding = game.Workspace.Sounds.Ding             -- sound
local part = game.Workspace.Configuration.DesertScout1  -- name
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local UserInputService = game:GetService("UserInputService")
local PlayerGui = player:WaitForChild("PlayerGui")
local toolup = PlayerGui:WaitForChild("clickE")
local maxpickup = 20
local gui = PlayerGui.HatUI.DesertScoutAdd



    UserInputService.InputBegan:Connect(function(input,gameProccesedEvent)

        local item = mouse.Target
        local distance = player:DistanceFromCharacter(item.Position)
if mouse.Target == part and enabled == true and distance <= maxpickup and input.UserInputType == Enum.UserInputType.MouseButton1 then
        ding:Play()
        gui.Enabled = true
        part.Position = Vector3.new(100,100,100)
        enabled = false

        part.Transparency = 1
        part.CanCollide = false

        game.ReplicatedStorage.HatEvent.DesertScoutEvent:FireServer()

wait(1)
        gui.Enabled = false
        enabled = true
        part.Transparency = 0
        part.CanCollide = true


end
    end)

It also runs when any button on the keyboard is presses.

This discussion is preserved as it was. New answers, comments, and votes are closed.