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

Why does it randomly stop allowing you to punch?

Asked by 4 years ago

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)

local UserInputService = game:GetService("UserInputService")

local function onInputEnded(inputObject, gameProcessedEvent)
    -- First check if the 'gameProcessedEvent' is true
    -- This indicates that another script had already processed the input, so this one can be ignored
    if gameProcessedEvent then return end
    -- Next, check that the input was a keyboard event
    if inputObject.UserInputType == Enum.UserInputType.Keyboard then
        print("A key was released: " .. inputObject.KeyCode.Name)
        if inputObject.KeyCode.Name == script.Parent.Parent.Backpack.Keys.PunchKey.Value then

            if script.Parent.Parent.Backpack.UsingAbility.Value == false and script.Parent.Parent.Backpack.Hitting.Value == false then
                    if game.Players.LocalPlayer.Backpack.Mana.Value > 10 then

                        print("ability")


                        local Anim =game.Players.LocalPlayer.Character.Humanoid:LoadAnimation(script:WaitForChild("PunchAnim",1))
                        print("AnimLoaded")
                        Anim:Play()
                        game.ReplicatedStorage.Special:FireServer("Punch")
                        print("using")
                        wait(0.4)
                        Anim:Stop()
                        wait(0.35)


                        print("finished")

                end
            end
        end
    end
end
UserInputService.InputEnded:Connect(onInputEnded)

Script (Inside of Server Script Service)

function ability(user, move)



    if move == "Punch" then
        user.Backpack.Mana.Value =  user.Backpack.Mana.Value - 10
        print(move.."was used by"..user.Name)
        local char = user.Character


        user.Backpack.UsingAbility.Value = true
        char.RightHand.Touched:Connect(function(hit)
            if user.Backpack.Hitting.Value == false then
        print("slap")






                if hit.Name ~= "Shader" or hit.ClassName ~= "Accessory" then

                    user.Backpack.Hitting.Value = true
                    hit.Parent.Humanoid.Health = hit.Parent.Humanoid.Health - 10

                elseif hit.Name == "Shader" or hit.Name == "Accessory" then
                        hit.Parent.Parent.Humanoid.Health = hit.Parent.Parent.Humanoid.Health - 10
                        user.Backpack.Hitting.Value = true
                end



            end


        end)
        print("Donehitting")
        wait(0.75)
        user.Backpack.Hitting.Value = false
        print("falsed hitting")
        user.Backpack.UsingAbility.Value = false    
    end


end
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!

Answer this question