Help me to fix my issue

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)
3 Likes

Looks like you need to delete the hitbox every click or so…

3 Likes

Alr im going to try it, but what i need to do instead of hitbox?

You should be creating and deleting the hitbox in a script. That way the touched event can no longer be used.

2 Likes

I dont get it but here is the hitbox script

local SS = game:GetService("ServerStorage")
local Hitbox = SS.Hitbox

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(Character)
		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)

You’re cloning it every time the player joins, instead you should be creating and deleting it when you activate the push.

2 Likes

Ok i am going to do that and i will let you know if that works

1 Like

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.

1 Like

I did it but now its detecting late how could i fix that

Watch Final Result | Streamable

Btw here is the updated 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

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)
1 Like