#Connected Script doesnt disconnect properly

34 messages · Page 1 of 1 (latest)

sly folio
#

of

#

course

#
lua BasicAtack.OnServerEvent:Connect(function(player)
    local Combat = player.Backpack:WaitForChild("Combat Values")
    local t1 = Combat:WaitForChild("Atkt1")
    local t2 = os.time()
    local IsBusy = Combat:WaitForChild("IsBusy")
    
    if os.difftime(t2, t1.Value) > 0.4 and IsBusy.Value == false then
        print("Punch")
        
        IsBusy.Value = true
        
        local Character = player.Character or player.CharacterAdded:Wait()
        local Humanoid = Character:WaitForChild("Humanoid")
        local LeftHand = Humanoid.Parent:WaitForChild("LeftHand")
        
        local TouchedConnection
        
        t1.Value = os.time()
        
        TouchedConnection = LeftHand.Touched:Connect(function(hit)
            if hit.Parent:FindFirstChildOfClass("Humanoid") and os.difftime(os.time(), t1.Value) > 0.4 then
                local HumanoidEnemy = hit.Parent:FindFirstChildOfClass("Humanoid")
                if HumanoidEnemy ~= Humanoid then
                    print(1)
                    HumanoidEnemy:TakeDamage(5)
                    TouchedConnection:Disconnect()
                else
                    print(2)
                    TouchedConnection:Disconnect()
                end
            else
                print(3)
                TouchedConnection:Disconnect()
            end
        end)

        wait(0.1)
        
        IsBusy.Value = false

        -- Disconnect the Touched event after the punch action is over
        if TouchedConnection then
            TouchedConnection:Disconnect()
        end
    end
end)
#

Touched event is connected after the punch action has been initiated and it remains connected until a hit is registered

ionic basin
#

Let me look intro that

sly folio
#

intro?

#

I make outros

ionic basin
#

into*

#

Its just my poor english, sorry

sly folio
#

I joke man, I understand what u said

#

I dont speak correctly

#

no one do

#

its boring to speak correctly

#

never say sorry

ionic basin
#

alright

ionic basin
sly folio
#

if the punch action is over, if the player comes into contact with an enemy afterwards, it will still register as a hit and deal damage

ionic basin
#

Thanks again man

sly folio
#

also use guard clauses

#

to make your code more readable

ionic basin
#

Okay

#

how to make it btw?

#

Cannot find somewhy

sly folio
#

to make

#

what

ionic basin
#

oh i get how

#

Copied your text and seen

sly folio
# ionic basin Copied your text and seen
BasicAtack.OnServerEvent:Connect(function(player)
    local Combat = player.Backpack:WaitForChild("Combat Values")
    local t1 = Combat:WaitForChild("Atkt1")
    local t2 = os.time()
    local IsBusy = Combat:WaitForChild("IsBusy")
    
    -- Guard clause for time difference and IsBusy check
    if os.difftime(t2, t1.Value) <= 0.4 or IsBusy.Value == true then
        return
    end

    print("Punch")
    
    IsBusy.Value = true
    
    local Character = player.Character or player.CharacterAdded:Wait()
    local Humanoid = Character:WaitForChild("Humanoid")
    local LeftHand = Humanoid.Parent:WaitForChild("LeftHand")
    
    local TouchedConnection
    
    t1.Value = os.time()
    
    TouchedConnection = LeftHand.Touched:Connect(function(hit)
        -- Guard clause for hit check and time difference check
        if not hit.Parent:FindFirstChildOfClass("Humanoid") or os.difftime(os.time(), t1.Value) <= 0.4 then
            print(3)
            TouchedConnection:Disconnect()
            return
        end

        local HumanoidEnemy = hit.Parent:FindFirstChildOfClass("Humanoid")

        -- Guard clause for self-hit check
        if HumanoidEnemy == Humanoid then
            print(2)
            TouchedConnection:Disconnect()
            return
        end

        print(1)
        HumanoidEnemy:TakeDamage(5)
        TouchedConnection:Disconnect()
    end)

    wait(0.1)
    
    IsBusy.Value = false

    -- Disconnect the Touched event after the punch action is over
    if TouchedConnection then
        TouchedConnection:Disconnect()
    end
end)
``` That's an example of a code that uses "Guard Clauses" its much more readable code
ionic basin
#
print(1)
#

yey

#

Bye man, have a nice day