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
01 | canDoDmg = true |
02 | dmg = 25 |
03 |
04 | local Plr = game.Players.LocalPlayer |
05 | local UserInputService = game:GetService( "UserInputService" ) |
06 | local Char = Plr.Character or Plr.CharacterAdded:Wait() |
07 | local Humanoid = Char:WaitForChild( "Humanoid" ) |
08 | local Anim = Instance.new( "Animation" ) |
09 | Anim.AnimationId = "rbxassetid://6203475027" |
10 | local track = Humanoid:LoadAnimation(Anim) |
11 |
12 | UserInputService.InputBegan:Connect( function (InputObject) |
13 | if InputObject.UserInputType = = Enum.UserInputType.MouseButton 1 then |
14 | track:Play() |
15 | script.Parent.RightHand.Touched:connect( function (hit) |
A number of issues, and they can be easily solved
01 | local canDoDmg = false |
02 | local dmg = 25 |
03 | local db = false |
04 |
05 | local Plr = game.Players.LocalPlayer |
06 | local UserInputService = game:GetService( "UserInputService" ) |
07 | local Char = Plr.Character or Plr.CharacterAdded:Wait() |
08 | local Humanoid = Char:WaitForChild( "Humanoid" ) |
09 | local Anim = Instance.new( "Animation" ) |
10 | Anim.AnimationId = "rbxassetid://6203475027" |
11 | local track = Humanoid:LoadAnimation(Anim) |
12 |
13 | UserInputService.InputBegan:Connect( function (InputObject) |
14 | if InputObject.UserInputType = = Enum.UserInputType.MouseButton 1 and db = = false then |
15 | track:Play() |
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
try this:
01 | canDoDmg = true |
02 | dmg = 25 |
03 |
04 | local Plr = game.Players.LocalPlayer |
05 | local UserInputService = game:GetService( "UserInputService" ) |
06 | local Char = Plr.Character or Plr.CharacterAdded:Wait() |
07 | local Humanoid = Char:WaitForChild( "Humanoid" ) |
08 | local Anim = Instance.new( "Animation" ) |
09 | Anim.AnimationId = "rbxassetid://6203475027" |
10 | local track = Humanoid:LoadAnimation(Anim) |
11 | local strike = false -- make a variable to stop spamming of our fire button |
12 |
13 | local TouchedEvent = nil -- make a container for our touched event so we can Disconnect() it later. |
14 | UserInputService.InputBegan:Connect( function (InputObject) |
15 | if InputObject.UserInputType = = Enum.UserInputType.MouseButton 1 then |
Hope this helps! :)