R to punch local script with magnitude

Hello, I’ve been struggling with this one script on punching with magnitude and converting the script to a keybind. If anyone could help me convert this script I would appreciate it heavily.

wait(2)

local Cooldown = false
local Player = game.Players.LocalPlayer
local Character = Player.Character
local Mouse = Player:GetMouse()
local Delay = .35

Mouse.Button1Down:Connect(function()
if Cooldown then return end
for _, v in pairs(Player.Character:GetChildren()) do
if v:IsA(“Tool”) then return end
end

spawn(function()
Cooldown = true
wait(Delay)
Cooldown = false
end)

script.RemoteEvent:FireServer()

end)

for _, v in pairs(Player.Character:GetChildren()) do
    if v:IsA(“Tool”) then return end
end

Instead of this you can just do

if Player.Character:FindFirstChildWhichIsA("Tool") then return end

As for converting this into a keybind, you can use the
KeyDown event instead of Button1Down. Example

local Mouse = game.Players.LocalPlayer:GetMouse()

Mouse.KeyDown:Connect(function(key)
    if key == "r" or key == "R" then
        print( "You pressed r!" )
    end
end)

What’s the point of this?

2 Likes

Thank you for your reply, been confused for the past day on this. Its for the script that’s connected to the remote function

1 Like

You can always look up mouse on the developer hub and check if it has an event that fires when you press a key, it’s a good habit to get into.

1 Like

thanks for the recommendation
[30 chars]

what order does this go in?
[30]

I’m confused, what order does what go in?

refer to original post and check the order of the script and refer back to your script

Mouse.KeyDown is deprecated. @fantasticsuit28 @C0lvy123

I suggest using UserInputService.InputBegan

local UserInputService = game:GetService("UserInputService")

local function OnInputBegan(Input, Busy)
    if (Busy) then return end --// Exit the function if the user is interacting with a GUI instance

    if (Input.KeyCode == Enum.KeyCode.E) then --// Check if they're pressing E
        print("Player Pressed E")
    end    
end

UserInputService.InputBegan:Connect(OnInputBegan)  --// Connect to InputBegan signal
4 Likes

I wasn’t giving a script, I was just showing an example of how to use KeyDown. It’s used just like any other event, if you’re not familiar with events I’m not sure how you wrote that script.