Stop Player from Gripping

		spawn(function()
					if Gripping == true then
					humanoid.WalkSpeed = 0
					humanoid.JumpPower = 0
					anim:Play()
					anim2:Play()
					local NewBlood = Blood:Clone()
					NewBlood.CFrame = char.HumanoidRootPart.CFrame * CFrame.new(0, 0, -1) * CFrame.Angles(0,0,0)
					local BloodParticle = NewBlood:WaitForChild("Particle")

					NewBlood.Parent = game.Workspace
					game.Debris:AddItem(NewBlood,0.5)
					BloodParticle:Emit(75)
					
					enemychar.HumanoidRootPart.Anchored = true
					
					for i, anims in pairs(enemychar.Humanoid:GetPlayingAnimationTracks()) do

						if anims.Name == "Gripped" then
							anims:Stop()
						end
					end
					wait(2.5)
					humanoid.WalkSpeed = 16
					humanoid.JumpPower = 50
					enemychar:FindFirstChild("Knocked"):Destroy()
					enemychar.Humanoid.Health = 0
					enemychar.HumanoidRootPart.Anchored = false
					char.HumanoidRootPart.Anchored = false
					end
				end)

I have a gripping script where you grip a player when there knocked. I have a question on how would I stop the player from being gripped if the player is hit. I have a boolvalue named hit that enters the characters everytime there hint. Anything to help I would appreciate.

Okay so I’ll be using coroutines as they work like spawn(function() end), but with no delay and you can stop the script.

We’ll need to have a value for the coroutine so we can stop it from outside as well as a variable for the Hit Value.

local hitValue = -- Path to the hitValue
local gripCo -- This will be nill for now 

Coroutines work like :Connect(function) and spawn(function), but we wish to repeat the coroutine so we’ll have to give the function a name (as well as destroying the coroutine after it has ended).

Note, you probaly want to put this function under your variables and put parameters in. If they aren’t variables yet.

local function gripping (--[[char, humanoid, anim, anim2, enemychar]]) -- Naming the function so we can recall it multiple times.
	if Gripping == true then
		humanoid.WalkSpeed = 0
		humanoid.JumpPower = 0
		anim:Play()
		anim2:Play()
		local NewBlood = Blood:Clone()
		NewBlood.CFrame = char.HumanoidRootPart.CFrame * CFrame.new(0, 0, -1) * CFrame.Angles(0,0,0)
		local BloodParticle = NewBlood:WaitForChild("Particle")

		NewBlood.Parent = game.Workspace
		game.Debris:AddItem(NewBlood,0.5)
		BloodParticle:Emit(75)

		enemychar.HumanoidRootPart.Anchored = true

		for i, anims in pairs(enemychar.Humanoid:GetPlayingAnimationTracks()) do

			if anims.Name == "Gripped" then
				anims:Stop()
			end
		end
		wait(2.5)
		humanoid.WalkSpeed = 16
		humanoid.JumpPower = 50
		enemychar:FindFirstChild("Knocked"):Destroy()
		enemychar.Humanoid.Health = 0
		enemychar.HumanoidRootPart.Anchored = false
		char.HumanoidRootPart.Anchored = false
		gripCo = nil -- So we can check if it's ended if we ever wish to.
	end 
end)

After this we’ll have to create a coroutine as replacement for the spawn(function) (on the exact line of the spawn function).

Every time we wish the player to start gripping someone we run this code:

coGrip = coroutine.create(gripping)
coroutine.resume(coGrip --[[,char, humanoid, anim, anim2, enemychar]]) -- Replace this with the parameters of the function.

Note, you’ve to start the coroutine as well. That’s why we use coroutine.resume().

Now we need to have a function to remove the coroutine every time you get hit.

hitValue:GetPropertyChangedSignal("Value"):Connect(function()
	gripCo = nil -- Stops the coroutine function (completely).
end)

IMPORTANT, you’ll have to use another function to undo the changes (such as Anchoring the player and playing an Animation) with another function.

1 Like

Thank you, so much. I understand how to now.