Welcome to the Forums!
Join thousands of other Roblox members with a free account.
Create Forum Account

I need some help with a punch script

  • This website was created by Roblox members to replace the official forums (because Roblox shut them down) Join us if you like Roblox!

ExtendedWaranty

New Member
Reputation: 0 / 0 / 0
Oct 8, 2021
9
1
3
Roblox
Extended_Waranty
Hello I am currently working on a game and have added a punch script from the toolbox (for r6) and have had an issue that I haven't been able to fix.
the issue is that when your hit misses it keeps the damage built up (this is stackable) so if done a fair amount you could just rub up against another player and they would die in one hit. Also I haven't been able to figure out were to put a line of code to add a 3 second cooldown.
here is the script:


local Players = game:GetService("Players")

local UserInputService = game:GetService("UserInputService")

local ReplicatedStorage = game:GetService("ReplicatedStorage")

local ContentProvider = game:GetService("ContentProvider")

local localPlayer = Players.LocalPlayer



local damage =15



local debounce = false



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

if input.KeyCode == Enum.KeyCode.E and not gameProcessedEvent then

if not debounce then -- Checks if debounce == false

debounce = true



local humanoid = localPlayer.Character.Humanoid -- Gets the localPlayer's humanoid



if humanoid.RigType == Enum.HumanoidRigType.R6 then -- Checks if the localPlayer's humanoid is R6



local animation = Instance.new("Animation") -- Inserts an animation

animation.AnimationId = "rbxassetid://8034097075"

animation.Parent = humanoid



local animationToLoad = {animation}

ContentProvider:PreloadAsync(animationToLoad) -- Preloads the animation to prevent delay



local loadedAnimation = humanoid:LoadAnimation(animation) -- Loads the animation with the humanoid



loadedAnimation:Play() -- Plays the animation



local hitDebounce = false



local touch



local function onTouch(hit)

if hit.Parent:FindFirstChildWhichIsA("Humanoid") then -- Checks if the instance hit has a humanoid

if hit.Parent.Humanoid.Health > 0 and not hitDebounce then -- Checks if hitDebounce == false and the target's health > 0

hitDebounce = true



local targetCharacter = hit.Parent -- Gets the target character



ReplicatedStorage.KillHumanoidEvent:FireServer( -- Fires the ServerScript which makes the health of the target go down

targetCharacter.Humanoid, -- Target humanoid

script.PunchSound, -- Punch sound

damage -- Damage

)



touch:disconnect()

wait(1)

hitDebounce = false



end

end

end



touch = localPlayer.Character["Left Arm"].Touched:Connect(onTouch)
 

ElHomburgerGuy

If you won't let yourself fail, you won't improve
Reputation: 5 / 0 / 1
Apr 24, 2019
6,713
24
2,631
113
Anonymous in the USA
Roblox
PieroWasTaking
Remember to put the LocalScript inside StarterCharacterScripts which is inside of StarterPlayer, along with the animation inside the script itself.

Also add a RemoteEvent inside ReplicatedStorage so you can call it for to a Server Script in ServerScriptService I named the event PunchingRemoteEvent for this example.

ExampleLocalScript:
local UserInputService = game:GetService("UserInputService")
local Character = script.Parent
local Humanoid = Character:WaitForChild("Humanoid")
local Punch = Humanoid:LoadAnimation(script:WaitForChild("PunchAnimaton")) -- Change this to the name of your animation!
Local Punching = false
local Player = game.Players.LocalPlayer

Player.Character:WaitForChild("Right Arm").Touched:Connect(function(hit)
    if hit and hit.Parent:FindFirstChildWhichIsA("Humaniod") and Punching and hit.Parent.Humaniod ~= Player.Character.Humaniod then
        if Player.Character.Humaniod.Health > 0 then
            game.ReplicatedStorage.PunchingRemoteEvent:FireServer(hit.Parent.Humaniod) --Calls on the RemoteEvent in ReplicatedStorage which will then call SeverScriptService
        end
    end
end)

UserInputService.InputBegan:Connect(function(input, processed)
    if processed then return end
    if input.KeyCode == Enum.KeyCode.F then --pressing F fires the function
        if not Punching then
            Punching = true
            Punch:Play() -- Playing animation
            wait(3) -- Animation goes on for three seconds
            Punch:Stop()
            wait(3)
            Punching = false -- Able to punch again!
            end
        end

end)

And here's what you would put inside ServerScriptService.
DamageScriptSeverScriptSerivce:
game.ReplicatedStorage.PunchingRemoteEvent.OnServerEvent:Connect(function(player, humaniod)
    humaniod:TakeDamage(5) --amount of damage
end)
 

ExtendedWaranty

New Member
Reputation: 0 / 0 / 0
Oct 8, 2021
9
1
3
Roblox
Extended_Waranty
thank you this was very helpful by the way would you happen to know how I could add some knockback to the attack?

Remember to put the LocalScript inside StarterCharacterScripts which is inside of StarterPlayer, along with the animation inside the script itself.

Also add a RemoteEvent inside ReplicatedStorage so you can call it for to a Server Script in ServerScriptService I named the event PunchingRemoteEvent for this example.

ExampleLocalScript:
local UserInputService = game:GetService("UserInputService")
local Character = script.Parent
local Humanoid = Character:WaitForChild("Humanoid")
local Punch = Humanoid:LoadAnimation(script:WaitForChild("PunchAnimaton")) -- Change this to the name of your animation!
Local Punching = false
local Player = game.Players.LocalPlayer

Player.Character:WaitForChild("Right Arm").Touched:Connect(function(hit)
    if hit and hit.Parent:FindFirstChildWhichIsA("Humaniod") and Punching and hit.Parent.Humaniod ~= Player.Character.Humaniod then
        if Player.Character.Humaniod.Health > 0 then
            game.ReplicatedStorage.PunchingRemoteEvent:FireServer(hit.Parent.Humaniod) --Calls on the RemoteEvent in ReplicatedStorage which will then call SeverScriptService
        end
    end
end)

UserInputService.InputBegan:Connect(function(input, processed)
    if processed then return end
    if input.KeyCode == Enum.KeyCode.F then --pressing F fires the function
        if not Punching then
            Punching = true
            Punch:Play() -- Playing animation
            wait(3) -- Animation goes on for three seconds
            Punch:Stop()
            wait(3)
            Punching = false -- Able to punch again!
            end
        end

end)

And here's what you would put inside ServerScriptService.
DamageScriptSeverScriptSerivce:
game.ReplicatedStorage.PunchingRemoteEvent.OnServerEvent:Connect(function(player, humaniod)
    humaniod:TakeDamage(5) --amount of damage
end)
 

ExtendedWaranty

New Member
Reputation: 0 / 0 / 0
Oct 8, 2021
9
1
3
Roblox
Extended_Waranty
Also I'm just now testing the script and the punch wont activate fo you know what could maybe be causing this?
Depends, what is the error?

Cause on line 5 I wrote Local Punching = false and not local Punching = false which could lead to problems. 😅
that could maybe be it ill check now
Post automatically merged:

oh wait nvm that isn't it I fixed that earlier
 

ElHomburgerGuy

If you won't let yourself fail, you won't improve
Reputation: 5 / 0 / 1
Apr 24, 2019
6,713
24
2,631
113
Anonymous in the USA
Roblox
PieroWasTaking
Also I'm just now testing the script and the punch wont activate fo you know what could maybe be causing this?
Depends, what is the error?

Cause on line 5 I wrote Local Punching = false and not local Punching = false which could lead to problems. 😅
that could maybe be it ill check now
Post automatically merged:

oh wait nvm that isn't it I fixed that earlier
What does it say in the output?
 

ExtendedWaranty

New Member
Reputation: 0 / 0 / 0
Oct 8, 2021
9
1
3
Roblox
Extended_Waranty
Also I'm just now testing the script and the punch wont activate fo you know what could maybe be causing this?
Depends, what is the error?

Cause on line 5 I wrote Local Punching = false and not local Punching = false which could lead to problems. 😅
that could maybe be it ill check now
Post automatically merged:

oh wait nvm that isn't it I fixed that earlier
What does it say in the output?it seems
it seems to have an issue with the fourth line of code
 

ElHomburgerGuy

If you won't let yourself fail, you won't improve
Reputation: 5 / 0 / 1
Apr 24, 2019
6,713
24
2,631
113
Anonymous in the USA
Roblox
PieroWasTaking
Also I'm just now testing the script and the punch wont activate fo you know what could maybe be causing this?
Depends, what is the error?

Cause on line 5 I wrote Local Punching = false and not local Punching = false which could lead to problems. 😅
that could maybe be it ill check now
Post automatically merged:

oh wait nvm that isn't it I fixed that earlier
What does it say in the output?it seems
it seems to have an issue with the fourth line of code
Where did you put the script in Studio? Would you mind screenshotting it?
 

ExtendedWaranty

New Member
Reputation: 0 / 0 / 0
Oct 8, 2021
9
1
3
Roblox
Extended_Waranty
here you go
Post automatically merged:

here you go
Also I'm just now testing the script and the punch wont activate fo you know what could maybe be causing this?
Depends, what is the error?

Cause on line 5 I wrote Local Punching = false and not local Punching = false which could lead to problems. 😅
that could maybe be it ill check now
Post automatically merged:

oh wait nvm that isn't it I fixed that earlier
What does it say in the output?it seems
it seems to have an issue with the fourth line of code
Where did you put the script in Studio? Would you mind screenshotting it?
I sent it in a separate thread bc this one is starting to get quite messy
 

Attachments

  • Screenshot 2021-11-19 092927.png
    Screenshot 2021-11-19 092927.png
    18 KB · Views: 5
Last edited:

ElHomburgerGuy

If you won't let yourself fail, you won't improve
Reputation: 5 / 0 / 1
Apr 24, 2019
6,713
24
2,631
113
Anonymous in the USA
Roblox
PieroWasTaking
here you go
Post automatically merged:

here you go
Also I'm just now testing the script and the punch wont activate fo you know what could maybe be causing this?
Depends, what is the error?

Cause on line 5 I wrote Local Punching = false and not local Punching = false which could lead to problems. 😅
that could maybe be it ill check now
Post automatically merged:

oh wait nvm that isn't it I fixed that earlier
What does it say in the output?it seems
it seems to have an issue with the fourth line of code
Where did you put the script in Studio? Would you mind screenshotting it?
I sent it in a separate thread bc this one is starting to get quite messy
Fourth Line, got it. local Punch = Humanoid:LoadAnimation(script:WaitForChild("PunchAnimaton"))

What does the error say on the OutPut? Is it not finding the child?

Then it could be local Punch = Character:LoadAnimation(script:WaitForChild("PunchAnimaton"))

But I wanna know what kind of error you're dealing with.
 

ExtendedWaranty

New Member
Reputation: 0 / 0 / 0
Oct 8, 2021
9
1
3
Roblox
Extended_Waranty
I Tried the
here you go
Post automatically merged:

here you go
Also I'm just now testing the script and the punch wont activate fo you know what could maybe be causing this?
Depends, what is the error?

Cause on line 5 I wrote Local Punching = false and not local Punching = false which could lead to problems. 😅
that could maybe be it ill check now
Post automatically merged:

oh wait nvm that isn't it I fixed that earlier
What does it say in the output?it seems
it seems to have an issue with the fourth line of code
Where did you put the script in Studio? Would you mind screenshotting it?
I sent it in a separate thread bc this one is starting to get quite messy
Fourth Line, got it. local Punch = Humanoid:LoadAnimation(script:WaitForChild("PunchAnimaton"))

What does the error say on the OutPut? Is it not finding the child?

Then it could be local Punch = Character:LoadAnimation(script:WaitForChild("PunchAnimaton"))

But I wanna know what kind of error you're dealing with.


Screenshot 2021-11-22 133414.png
I Tried the first script you sent to no success the second one you sent triggered this error though