Why does it randomly stop allowing you to punch?
Hello, I have been making a punch script (For a fighting game) and so far it has went pretty well, but there's one problem, and that is that if you punch an enemy, sometimes it takes multiple hits, the player will be unable to punch, there are no error messages, and all I know is that it wont turn Hitting.Value to false. Everything else works fine. The layout for items you need is listed here:
LocalScript (Inside of Starter Player Scripts, Parents a animation)
01 | local UserInputService = game:GetService( "UserInputService" ) |
03 | local function onInputEnded(inputObject, gameProcessedEvent) |
06 | if gameProcessedEvent then return end |
08 | if inputObject.UserInputType = = Enum.UserInputType.Keyboard then |
09 | print ( "A key was released: " .. inputObject.KeyCode.Name) |
10 | if inputObject.KeyCode.Name = = script.Parent.Parent.Backpack.Keys.PunchKey.Value then |
12 | if script.Parent.Parent.Backpack.UsingAbility.Value = = false and script.Parent.Parent.Backpack.Hitting.Value = = false then |
13 | if game.Players.LocalPlayer.Backpack.Mana.Value > 10 then |
18 | local Anim = game.Players.LocalPlayer.Character.Humanoid:LoadAnimation(script:WaitForChild( "PunchAnim" , 1 )) |
21 | game.ReplicatedStorage.Special:FireServer( "Punch" ) |
35 | UserInputService.InputEnded:Connect(onInputEnded) |
Script (Inside of Server Script Service)
01 | function ability(user, move) |
05 | if move = = "Punch" then |
06 | user.Backpack.Mana.Value = user.Backpack.Mana.Value - 10 |
07 | print (move.. "was used by" ..user.Name) |
08 | local char = user.Character |
11 | user.Backpack.UsingAbility.Value = true |
12 | char.RightHand.Touched:Connect( function (hit) |
13 | if user.Backpack.Hitting.Value = = false then |
21 | if hit.Name ~ = "Shader" or hit.ClassName ~ = "Accessory" then |
23 | user.Backpack.Hitting.Value = true |
24 | hit.Parent.Humanoid.Health = hit.Parent.Humanoid.Health - 10 |
26 | elseif hit.Name = = "Shader" or hit.Name = = "Accessory" then |
27 | hit.Parent.Parent.Humanoid.Health = hit.Parent.Parent.Humanoid.Health - 10 |
28 | user.Backpack.Hitting.Value = true |
39 | user.Backpack.Hitting.Value = false |
40 | print ( "falsed hitting" ) |
41 | user.Backpack.UsingAbility.Value = false |
46 | game.ReplicatedStorage.Special.OnServerEvent:Connect(ability) |
There is a RemoteEvent named special in Replicated Storage
In Starter Pack there are these Values:
-Mana (Int)
-UsingAbility (Bool)
-Hitting (Bool)
I also used a cel shader, which is why it has to detect for "Shader"
I am a beginner at scripting, so it might be messy, sorry!