So i am trying to make a pushing system but when i tried it this thing happens and i dont know how to fix it
Here is the script:
local SoundSFXs = game.ReplicatedStorage:WaitForChild("SoundSFXs")
local CombatEvent = game.ReplicatedStorage.Events:WaitForChild("CombatEvent")
local Animations = game.ReplicatedStorage.Animations
local DebrisService = game:GetService("Debris")
local PlayersWhoCanPush = {}
local function LoadAnimation(Humanoid, Animation)
local Animation = Humanoid:LoadAnimation(Animation)
Animation:Play()
task.wait(1.5)
CombatEvent:FireClient(game.Players:GetPlayerFromCharacter(Humanoid.Parent), "CoolDownEnd")
end
local function PushHandler(Character, CharacterToDamage)
local Animation = CharacterToDamage.Humanoid:LoadAnimation(Animations.Knockback)
Animation:Play()
local Attachment = Instance.new("Attachment", CharacterToDamage.HumanoidRootPart)
local LinearVelocity = Instance.new("LinearVelocity", Attachment)
LinearVelocity.MaxForce = 99999
LinearVelocity.VectorVelocity = (Character.HumanoidRootPart.Position - CharacterToDamage.HumanoidRootPart.Position).Unit * Vector3.new(0,0,-40)
LinearVelocity.Attachment0 = Attachment
CharacterToDamage.HumanoidRootPart.Anchored = false
CharacterToDamage.HumanoidRootPart.CFrame *= CFrame.Angles(math.rad(180), 0, 0)
DebrisService:AddItem(Attachment, 0.1)
end
CombatEvent.OnServerEvent:Connect(function(Player, attackType, Cooldown)
local Character = Player.Character
table.insert(PlayersWhoCanPush, Character.Name)
if Character then
local Humanoid = Character:FindFirstChild("Humanoid")
if Humanoid then
if attackType == "Push" and table.find(PlayersWhoCanPush, Character.Name) then
LoadAnimation(Humanoid, Animations[attackType])
Character["Hitbox"].Touched:Connect(function(hit)
local CharacterToDamage = hit.Parent
local HumanoidToDamage = CharacterToDamage:FindFirstChild("Humanoid")
if HumanoidToDamage then
PushHandler(Character, CharacterToDamage)
end
end)
task.wait(0.1)
if table.find(PlayersWhoCanPush, Character.Name) then
table.remove(PlayersWhoCanPush, table.find(PlayersWhoCanPush, Character.Name))
end
end
end
end
end)
Like @Sulphural said, the hitbox is never getting deleted, so it will keep firing the Touched event, etc. etc. Another thing to note is that for each hitbox you are creating a connection and never disconnecting it. This can lead to memory leaks and more memory usage. You can fix this by changing :Connect() to :Once(), which disconnects the connection after it has been used once.
local SoundSFXs = game.ReplicatedStorage:WaitForChild("SoundSFXs")
local CombatEvent = game.ReplicatedStorage.Events:WaitForChild("CombatEvent")
local Animations = game.ReplicatedStorage.Animations
local DebrisService = game:GetService("Debris")
local PlayersWhoCanPush = {}
local function LoadAnimation(Humanoid, Animation)
local Animation = Humanoid:LoadAnimation(Animation)
Animation:Play()
task.wait(1.5)
CombatEvent:FireClient(game.Players:GetPlayerFromCharacter(Humanoid.Parent), "CoolDownEnd")
end
local function PushHandler(Character, CharacterToDamage)
local Animation = CharacterToDamage.Humanoid:LoadAnimation(Animations.Knockback)
Animation:Play()
local Attachment = Instance.new("Attachment", CharacterToDamage.HumanoidRootPart)
local LinearVelocity = Instance.new("LinearVelocity", Attachment)
LinearVelocity.MaxForce = 99999
LinearVelocity.VectorVelocity = (Character.HumanoidRootPart.Position - CharacterToDamage.HumanoidRootPart.Position).Unit * Vector3.new(0,0,-40)
LinearVelocity.Attachment0 = Attachment
CharacterToDamage.HumanoidRootPart.Anchored = false
CharacterToDamage.HumanoidRootPart.CFrame *= CFrame.Angles(math.rad(180), 0, 0)
DebrisService:AddItem(Attachment, 0.1)
end
local function CreateHitbox(Player)
print("Hello")
local SS = game:GetService("ServerStorage")
local Hitbox = SS.Hitbox
local Character = Player.Character
if Character then
local HitboxClone = Hitbox:Clone()
local Motor6D = Instance.new("Motor6D")
HitboxClone.Parent = Character
Motor6D.Parent = Character:FindFirstChild("Torso")
Motor6D.Part0 = Character.Torso
Motor6D.Part1 = HitboxClone
HitboxClone.Position = Character.HumanoidRootPart.Position
end
end
local function DeleteHitbox(Player)
local Character = Player.Character
if Character then
local Hitbox = Character:FindFirstChild("Hitbox")
if Hitbox then
Hitbox:Destroy()
end
end
end
CombatEvent.OnServerEvent:Connect(function(Player, attackType, Cooldown)
print("e")
local Character = Player.Character
table.insert(PlayersWhoCanPush, Character.Name)
if Character then
local Humanoid = Character:FindFirstChild("Humanoid")
if Humanoid then
if attackType == "Push" and table.find(PlayersWhoCanPush, Character.Name) then
CreateHitbox(Player)
LoadAnimation(Humanoid, Animations[attackType])
Character["Hitbox"].Touched:Connect(function(hit)
local CharacterToDamage = hit.Parent
local HumanoidToDamage = CharacterToDamage:FindFirstChild("Humanoid")
if HumanoidToDamage then
PushHandler(Character, CharacterToDamage)
end
end)
task.wait(0.1)
if table.find(PlayersWhoCanPush, Character.Name) then
table.remove(PlayersWhoCanPush, table.find(PlayersWhoCanPush, Character.Name))
DeleteHitbox(Player)
end
end
end
end
end)