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

Script no longer run after humanoid's death?

Asked by 3 years ago

So, I'm making a combat system with various attacks/functions but I just discovered that the attacks no longer run after the humanoid has been killed. What might be the case here?

If you're curious about the scripts;

Local Script under StarterPack:

local UIS = game:GetService("UserInputService")

UIS.InputBegan:Connect(function(input, gameProcessed)
    if input.KeyCode == Enum.KeyCode.Q then
        game.ReplicatedStorage.RemoteLP:FireServer()        
    end

    if input.KeyCode == Enum.KeyCode.E then
        game.ReplicatedStorage.RemoteRP:FireServer()        
    end

    if input.KeyCode == Enum.KeyCode.Z then
        game.ReplicatedStorage.RemoteLK:FireServer()        
    end

    if input.KeyCode == Enum.KeyCode.C then
        game.ReplicatedStorage.RemoteRK:FireServer()        
    end 
end)

Server Script under ServerScriptStorage:

game.Players.PlayerAdded:Connect(function(player)
    local player = player
    local character = player.Character or player.CharacterAdded:Wait()
    local humanoid = character:WaitForChild("Humanoid")

    local debounce = false

    local leftPunching = false
    local rightPunching = false
    local leftKicking = false
    local rightKicking = false

    local LPcooldown = false
    local RPcooldown = false 
    local LKcooldown = false
    local RKcooldown = false

    local function leftPunch()
        local leftPunch = script.LeftPunch
        local leftPunchTrack = humanoid:LoadAnimation(leftPunch)
        local leftFist = character:WaitForChild("Left Arm")

        if not LPcooldown then
            LPcooldown = true

            leftPunching = true

            leftPunchTrack:Play()

            wait(1)

            LPcooldown = false

            leftPunching = false
        end 

        leftFist.Touched:Connect(function(hit)
            local humanoid = hit.Parent:FindFirstChildWhichIsA("Humanoid")

            if humanoid and leftPunching then
                if not debounce then
                    debounce = true

                    humanoid:TakeDamage(10)

                    wait(.5)
                    debounce = false
                end
            end
        end)
    end

    local function rightPunch()
        local rightPunch = script.RightPunch
        local rightPunchTrack = humanoid:LoadAnimation(rightPunch)
        local rightFist = character:WaitForChild("Right Arm")

        if not RPcooldown then
            RPcooldown = true   

            rightPunching = true

            rightPunchTrack:Play()  

            wait(1)

            RPcooldown = false

            rightPunching = false
        end 

        rightFist.Touched:Connect(function(hit)
            local humanoid = hit.Parent:FindFirstChildWhichIsA("Humanoid")

            if humanoid and rightPunching then  
                if not debounce then
                    debounce = true

                    humanoid:TakeDamage(10)

                    wait(.5)
                    debounce = false
                end
            end
        end)
    end 

    local function leftKick()
        local leftKick = script.LeftKick
        local leftKickTrack = humanoid:LoadAnimation(leftKick)
        local leftFoot = character:WaitForChild("Left Leg")

        if not LKcooldown then
            LKcooldown = true

            leftKicking = true

            leftKickTrack:Play()

            wait(1)

            LKcooldown = false

            leftKicking = false
        end

        leftFoot.Touched:Connect(function(hit)
            local humanoid = hit.Parent:FindFirstChildWhichIsA("Humanoid")

            if humanoid and leftKicking then
                if not debounce then
                    debounce = true

                    humanoid:TakeDamage(20)

                    wait(.5)
                    debounce = false
                end
            end
        end)
    end

    local function rightKick()
        local rightKick = script.RightKick
        local rightKickTrack = humanoid:LoadAnimation(rightKick)
        local rightFoot = character:WaitForChild("Right Leg")

        if not RKcooldown then
            RKcooldown = true

            rightKicking = true

            rightKickTrack:Play()

            wait(1)

            RKcooldown = false

            rightKicking = false
        end

        rightFoot.Touched:Connect(function(hit)
            local humanoid = hit.Parent:FindFirstChildWhichIsA("Humanoid")

            if humanoid and rightKicking then
                if not debounce then
                    debounce = true

                    humanoid:TakeDamage(20)

                    wait(.5)
                    debounce = false
                end
            end
        end)
    end

    game.ReplicatedStorage.RemoteLP.OnServerEvent:Connect(leftPunch)
    game.ReplicatedStorage.RemoteRP.OnServerEvent:Connect(rightPunch)
    game.ReplicatedStorage.RemoteLK.OnServerEvent:Connect(leftKick)
    game.ReplicatedStorage.RemoteRK.OnServerEvent:Connect(rightKick)
end)
0
This is because you only ever allocated the Humanoid once; the Humanoid Instance becomes null as the new Character is loaded in after "death". You should be locally allocating the Humanoid in your "action" functions to ensure you're always using the present Instance. Ziffixture 6913 — 3y

1 answer

Log in to vote
0
Answered by 3 years ago

I've already solved it. Thanks much to @Ziffixture for teaching me the ways.

Server Script:

game.Players.PlayerAdded:Connect(function(player)
    local player = player

    local debounce = false

    local leftPunching = false
    local rightPunching = false
    local leftKicking = false
    local rightKicking = false

    local LPcooldown = false
    local RPcooldown = false 
    local LKcooldown = false
    local RKcooldown = false

    local function leftPunch()
        local character = player.Character or player.CharacterAdded:Wait()
        local humanoid = character:WaitForChild("Humanoid")

        local leftPunch = script.LeftPunch
        local leftPunchTrack = humanoid:LoadAnimation(leftPunch)
        local leftFist = character:WaitForChild("Left Arm")

        if not LPcooldown then
            LPcooldown = true

            leftPunching = true

            leftPunchTrack:Play()

            wait(1)

            LPcooldown = false

            leftPunching = false
        end 

        leftFist.Touched:Connect(function(hit)
            local humanoid = hit.Parent:FindFirstChildWhichIsA("Humanoid")

            if humanoid and leftPunching then
                if not debounce then
                    debounce = true

                    humanoid:TakeDamage(10)

                    wait(.5)
                    debounce = false
                end
            end
        end)
    end

    local function rightPunch()
        local character = player.Character or player.CharacterAdded:Wait()
        local humanoid = character:WaitForChild("Humanoid")

        local rightPunch = script.RightPunch
        local rightPunchTrack = humanoid:LoadAnimation(rightPunch)
        local rightFist = character:WaitForChild("Right Arm")

        if not RPcooldown then
            RPcooldown = true   

            rightPunching = true

            rightPunchTrack:Play()  

            wait(1)

            RPcooldown = false

            rightPunching = false
        end 

        rightFist.Touched:Connect(function(hit)
            local humanoid = hit.Parent:FindFirstChildWhichIsA("Humanoid")

            if humanoid and rightPunching then  
                if not debounce then
                    debounce = true

                    humanoid:TakeDamage(10)

                    wait(.5)
                    debounce = false
                end
            end
        end)
    end 

    local function leftKick()
        local character = player.Character or player.CharacterAdded:Wait()
        local humanoid = character:WaitForChild("Humanoid")

        local leftKick = script.LeftKick
        local leftKickTrack = humanoid:LoadAnimation(leftKick)
        local leftFoot = character:WaitForChild("Left Leg")

        if not LKcooldown then
            LKcooldown = true

            leftKicking = true

            leftKickTrack:Play()

            wait(1)

            LKcooldown = false

            leftKicking = false
        end

        leftFoot.Touched:Connect(function(hit)
            local humanoid = hit.Parent:FindFirstChildWhichIsA("Humanoid")

            if humanoid and leftKicking then
                if not debounce then
                    debounce = true

                    humanoid:TakeDamage(20)

                    wait(.5)
                    debounce = false
                end
            end
        end)
    end

    local function rightKick()
        local character = player.Character or player.CharacterAdded:Wait()
        local humanoid = character:WaitForChild("Humanoid")

        local rightKick = script.RightKick
        local rightKickTrack = humanoid:LoadAnimation(rightKick)
        local rightFoot = character:WaitForChild("Right Leg")

        if not RKcooldown then
            RKcooldown = true

            rightKicking = true

            rightKickTrack:Play()

            wait(1)

            RKcooldown = false

            rightKicking = false
        end

        rightFoot.Touched:Connect(function(hit)
            local humanoid = hit.Parent:FindFirstChildWhichIsA("Humanoid")

            if humanoid and rightKicking then
                if not debounce then
                    debounce = true

                    humanoid:TakeDamage(20)

                    wait(.5)
                    debounce = false
                end
            end
        end)
    end

    game.ReplicatedStorage.RemoteLP.OnServerEvent:Connect(leftPunch)
    game.ReplicatedStorage.RemoteRP.OnServerEvent:Connect(rightPunch)
    game.ReplicatedStorage.RemoteLK.OnServerEvent:Connect(leftKick)
    game.ReplicatedStorage.RemoteRK.OnServerEvent:Connect(rightKick)
end)
Ad

Answer this question