Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How can i make this punch script click to punch?

Asked by
hh9man 5
6 years ago

I made a script that punches when you press q, but i want to know how i can make it a click to punch script instead.

local UserInputService = game:GetService("UserInputService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local punchEvent = ReplicatedStorage:WaitForChild("PunchEvent")

local ready = true

local function punch(inputObject, gameProcessed)
    if inputObject.KeyCode == Enum.KeyCode.Q and ready then
        punchEvent:FireServer()
        ready = false
        wait(0.5)
        ready = true
    end
end

UserInputService.InputBegan:Connect(punch)

2 answers

Log in to vote
0
Answered by 6 years ago
Edited 6 years ago

You would find the player's mouse using :GetMouse(). Then, you would use a click function through Button1Down.

Local Script:

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local punchEvent = ReplicatedStorage:WaitForChild("PunchEvent")

local ready = true

local function punch()
    if ready then
        punchEvent:FireServer()
        ready = false
        wait(0.5)
        ready = true
    end
end

mouse.Button1Down:Connect(punch)

0
Thanks. hh9man 5 — 6y
Ad
Log in to vote
0
Answered by
chomboghai 2044 Moderation Voter Community Moderator
6 years ago
Edited 6 years ago

All you would have to do is change line 8, you are currently checking if the input is Q. Check if the UserInputType == Enum.UserInputType.MouseButton1 because MouseButton1 is left click.

local function punch(inputObject, gameProcessed)
    if inputObject.UserInputType == Enum.UserInputType.MouseButton1 and ready then
        -- rest of code

EDIT:

I'd recommend going through with InfernoExeuctioner's idea, using the mouse would eliminate your need for using UserInputService at all.

Answer this question