#Connected Script doesnt disconnect properly
34 messages · Page 1 of 1 (latest)
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
Let me look intro that
I joke man, I understand what u said
I dont speak correctly
no one do
its boring to speak correctly
never say sorry
alright
but why havent it closing when it must go to else parts
Well thanks for help
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
oh, okay, i get you
Thanks again man
Guard clauses, ak making my code look like yours
oh i get how
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