Code not successfully anchoring root part

after M1s 1,2,3,4, when it loops back to 1 it stops anchoring. So like Click, Click, Click, Click (anchors fine), Cooldown, Click Click Click Click (stops anchoring)

local rs = game:GetService("ReplicatedStorage")
local Tweeninfo = TweenInfo.new(0.3, Enum.EasingStyle.Exponential, Enum.EasingDirection.In, 0, false)
local TS = game:GetService("TweenService")


rs.DamageDealer.OnServerEvent:Connect(function(plr, attackcycle, cooldown)
	local Params = RaycastParams.new()
	Params.FilterType = Enum.RaycastFilterType.Exclude
	Params.FilterDescendantsInstances = {plr.Character}
	local rayDistance = 10 --// 3 studs
	local origin = plr.Character:FindFirstChild("HumanoidRootPart").Position --// Position of the player
	local direction = plr.Character:FindFirstChild("HumanoidRootPart").CFrame.LookVector * rayDistance --// Direction the player is looking * distance
	local raycast = workspace:Raycast(origin, direction ,Params)

	print(raycast)
	print(plr.Character:FindFirstChild("Head").Orientation.X)


	if attackcycle == 1 then
		plr.Character:FindFirstChild("HumanoidRootPart").Anchored = true
		local Football = game:GetService("ReplicatedStorage").Football:Clone()
		Football.Parent = workspace
		Football.Position = plr.Character:WaitForChild("Right Leg").Position 

		local FootballTween = TS:Create(Football, Tweeninfo, {["Position"]=(raycast and raycast.Position or origin + direction)})
		FootballTween:Play()
		FootballTween.Completed:Connect(function()
			Football:Destroy()
			task.wait(0.2)
			plr.Character:FindFirstChild("HumanoidRootPart").Anchored = false
		end)
	end

	if attackcycle == 2 then
		plr.Character:FindFirstChild("HumanoidRootPart").Anchored = true
		local Football = game:GetService("ReplicatedStorage").Football:Clone()
		Football.Parent = workspace
		Football.Position = plr.Character:WaitForChild("Left Leg").Position
		local FootballTween = TS:Create(Football, Tweeninfo, {["Position"]=(raycast and raycast.Position or origin + direction)})
		FootballTween:Play()
		FootballTween.Completed:Connect(function()
			Football:Destroy()
			task.wait(0.2)
			plr.Character:FindFirstChild("HumanoidRootPart").Anchored = false
		end)
	end

	if attackcycle == 3 then
		plr.Character:FindFirstChild("HumanoidRootPart").Anchored = true
		local Football = game:GetService("ReplicatedStorage").Football:Clone()
		local SpecialTweeninfo = TweenInfo.new(0.15, Enum.EasingStyle.Exponential, Enum.EasingDirection.In, 0, false)
		Football.Parent = workspace
		Football.Position = plr.Character:WaitForChild("Right Leg").Position 

		local FootballTween = TS:Create(Football, SpecialTweeninfo, {["Position"]=(raycast and raycast.Position or origin + direction)})
		FootballTween:Play()

		FootballTween.Completed:Connect(function()
			FootballTween:Play()
			task.wait(0.3)
			Football:Destroy()
			task.wait(1)
			plr.Character:FindFirstChild("HumanoidRootPart").Anchored = false
		end)


	end

	if attackcycle == 4 then
		plr.Character:FindFirstChild("HumanoidRootPart").Anchored = true
		local Football = game:GetService("ReplicatedStorage").Football:Clone()
		local SpecialTweeninfo = TweenInfo.new(0.2, Enum.EasingStyle.Exponential, Enum.EasingDirection.In, 0, false)
		Football.Parent = workspace
		Football.Position = plr.Character:WaitForChild("Right Leg").Position 

		local FootballTween = TS:Create(Football, SpecialTweeninfo, {["Position"]=(raycast and raycast.Position or origin + direction)})
		FootballTween:Play()

		FootballTween.Completed:Connect(function()
			FootballTween:Play()
			task.wait(0.2)
			Football:Destroy()
			task.wait(0.3)
			plr.Character:FindFirstChild("HumanoidRootPart").Anchored = false
		end)


	end



	--detects if it hit a part of a character with a humanoid
	if raycast and raycast.Instance.Parent:FindFirstChild("Humanoid") then
		local hum = raycast.Instance.Parent:FindFirstChild("Humanoid")
		local char = hum.Parent
		local humanoid = char:FindFirstChild("Humanoid")
		char:FindFirstChild("HumanoidRootPart").Anchored = true
		if attackcycle == 1 or 2 then
			humanoid:TakeDamage(5)
		elseif attackcycle == 3 then
			humanoid:TakeDamage(10)
		elseif attackcycle == 4 then
			humanoid:TakeDamage(15)
		end
		task.wait(0.5)
		char:FindFirstChild("HumanoidRootPart").Anchored = false
	end
end)

Anyone? A reply would be much appreciated

Any errors, messages that show in the output window?

Sorry but this script is a complete mess, there’s no wonder you can’t find the problem.

  • You need to use variables for storing the player’s HumanoidRootPart so that you aren’t constantly using :FindFirstChild
  • There’s an unused “cooldown” parameter.
  • You are using “rs” as a variable for ReplicatedStorage and yet you use game:GetService(“ReplicatedStorage”) all throughout the function
  • Many lines could be taken out of the if statements to reduce the amount of repeated code
  • Theres no indication of any cooldown or debounce being used here, but task.wait is being used all over the place, meaning the player could fire this as often as they wanted and completely mess up the timing of any tweens or anchorings

Despite all that, I’m sure the problem lies here:

FootballTween.Completed:Connect(function()
   		FootballTween:Play()
   		task.wait(0.3)
   		Football:Destroy()
   		task.wait(1)
   		plr.Character:FindFirstChild("HumanoidRootPart").Anchored = false
   	end)

The tween will play itself again once completed, and then 1.3 seconds later it unanchors the root part, which means this part of the code repeats forever and keeps unanchoring the root every 1.3 seconds.

Thus you shouldn’t use:

.Completed:Connect()

But rather:

.Completed:Once()

Or remove the second time you play the tween entirely

cooldown isn’t unused. it is from my other script that triggers the event and I put it there in case I needed it in this one (Which I didn’t)

I couldn’t be bothered to make a variable for humanoid root part ik im lazy and counter-productive mbmb

I think I understand the problem now, thx ill try

@jellolemo thanks for the help,

Completed:Once() 

worked