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

How do i fix my punch script?

Asked by 4 years ago
Edited 4 years ago

Theres a few problems with my script 1. The animation is spammable 2. the player does damage just by walking up to a player when the right hand touches. But i want it to only do damage when the punch animation is played

01canDoDmg = true
02dmg = 25
03 
04local Plr = game.Players.LocalPlayer
05local UserInputService = game:GetService("UserInputService")
06local Char = Plr.Character or Plr.CharacterAdded:Wait()
07local Humanoid = Char:WaitForChild("Humanoid")
08local Anim = Instance.new("Animation")
09Anim.AnimationId = "rbxassetid://6203475027"
10local track = Humanoid:LoadAnimation(Anim)
11 
12UserInputService.InputBegan:Connect(function(InputObject)
13    if InputObject.UserInputType == Enum.UserInputType.MouseButton1 then
14        track:Play()
15        script.Parent.RightHand.Touched:connect(function(hit)
View all 22 lines...

2 answers

Log in to vote
1
Answered by 4 years ago
Edited 4 years ago

A number of issues, and they can be easily solved

01local canDoDmg = false
02local dmg = 25
03local db = false
04 
05local Plr = game.Players.LocalPlayer
06local UserInputService = game:GetService("UserInputService")
07local Char = Plr.Character or Plr.CharacterAdded:Wait()
08local Humanoid = Char:WaitForChild("Humanoid")
09local Anim = Instance.new("Animation")
10Anim.AnimationId = "rbxassetid://6203475027"
11local track = Humanoid:LoadAnimation(Anim)
12 
13UserInputService.InputBegan:Connect(function(InputObject)
14    if InputObject.UserInputType == Enum.UserInputType.MouseButton1 and db == false then
15        track:Play()
View all 29 lines...

Explanation of what I changed: I added a debounce / cooldown to you code to make it so that you cannot spam it (look for the delay thing inside the script) Also made it so that you can only do damage when you punch Also added local in your variables, good coding practice

Let me know if you have any errors

Ad
Log in to vote
0
Answered by
TGazza 1336 Moderation Voter
4 years ago

try this:

01canDoDmg = true
02dmg = 25
03 
04local Plr = game.Players.LocalPlayer
05local UserInputService = game:GetService("UserInputService")
06local Char = Plr.Character or Plr.CharacterAdded:Wait()
07local Humanoid = Char:WaitForChild("Humanoid")
08local Anim = Instance.new("Animation")
09Anim.AnimationId = "rbxassetid://6203475027"
10local track = Humanoid:LoadAnimation(Anim)
11local strike = false -- make a variable to stop spamming of our fire button
12 
13local TouchedEvent = nil -- make a container for our touched event so we can Disconnect() it later.
14UserInputService.InputBegan:Connect(function(InputObject)
15    if InputObject.UserInputType == Enum.UserInputType.MouseButton1 then
View all 34 lines...

Hope this helps! :)

0
This one does damage to the player just by walking up to them without playing the animation ItzSulfonic 61 — 4y

Answer this question