Help needing how to script a grab move/grab attack

Hello there! I just recently starting scripting my own game after a few weeks of learning LUA. My scripting is pretty basic so go easy on me please :slight_smile: I’m trying to learn how to script a grab attack like the source I shown below. Animations are already done, the hard part is just how to script is cause I was unable to find any good sources on how to. Any idea on how I can do this fellow developers?
Also something Something similar to this too
2024-08-1307-20-06online-video-cutter.com-ezgif.com-speed

1 Like

I was thinking of doing it sorta like this:

local remoteEvent = game:GetService("ReplicatedStorage").RemoteEvent
local PhysicsService = game:GetService("PhysicsService")

remoteEvent.OnServerEvent:Connect(function(client, data)
	if data.Ability == 1 then
		
		local char = client.Character
		local collisionGroup = "PlayerCharacters"

		local animationTrack = char:FindFirstChild("Humanoid"):LoadAnimation("your animation ID here")

		PhysicsService:RegisterCollisionGroup(collisionGroup)
		PhysicsService:CollisionGroupSetCollidable(collisionGroup,collisionGroup, false)
		
		for _, parts in pairs(client.Character:GetChildren()) do
			if parts:IsA("BasePart") then
				parts.CollisionGroup = collisionGroup
			end
		end
		
		local targetChar = workspace.Rig -- replace with your target
		
		for _, parts in pairs(targetChar:GetChildren()) do
			if parts:IsA("BasePart") then
				parts.CollisionGroup = collisionGroup
			end
		end
		
		targetChar:FindFirstChild("HumanoidRootPart").CFrame = client.Character:FindFirstChild("HumanoidRootPart").CFrame * CFrame.new(0,2,-2)
		
		local weldConstraint = Instance.new("WeldConstraint")
		weldConstraint.Parent = client.Character:FindFirstChild("UpperTorso")
		weldConstraint.Part0 = client.Character:FindFirstChild("UpperTorso")
		weldConstraint.Part1 = targetChar:FindFirstChild("HumanoidRootPart")
		
		char:FindFirstChild("Humanoid").WalkSpeed = 0
		char:FindFirstChild("Humanoid").JumpPower = 0
		
		--Play animation
		
		animationTrack:Play()
		
		--Listen to when the animation ends and the set the humanoid walkspeed to 16 and jumpPower to 50

		animationTrack.Ended:Connect(function()
			weldConstraint:Destroy()
			PhysicsService:CollisionGroupSetCollidable(collisionGroup,collisionGroup, true)
			char:FindFirstChild("Humanoid").WalkSpeed = 16
			char:FindFirstChild("Humanoid").JumpPower = 50
		end)
	end
end)

For testing purposes i used a local script which detected when the player pressed the key “E” and using a remote event i send a varibale called Ability (you don’t have to do this i just explain what i did to test it) and the rest of the code happened on a server script inside the serverscriptservice. Let me know if it works cuz i did not have time to test it with an animation

I was unable to get it to work, I used your code as reference to test it and i’m getting an error that says server script service, attempt to nil with “ability”. The local script which I made works fine but the server script itself that you sent me was not able to work. If you could gladly tell me what I did wrong, would greatly appreciate it, I can show you where I put my code at too.

--My Local Script which I put in starer player scripts--
local RS = game:GetService("ReplicatedStorage")
local UIS = game:GetService("UserInputService")
local RemoteEvent = RS.RemoteEvent

UIS.InputBegan:Connect(function(input, processed)
	if input.UserInputType == Enum.UserInputType.Keyboard and processed == false then
		if input.KeyCode == Enum.KeyCode.G then
			print("G")
			RemoteEvent:FireServer()
		end
	end
end)

--The Server Script you sent me and that I modified for testing, I put it in server script service--
local remoteEvent = game:GetService("ReplicatedStorage").RemoteEvent
local PhysicsService = game:GetService("PhysicsService")

remoteEvent.OnServerEvent:Connect(function(client, data)
	if data.Ability == 1 then

		local char = client.Character
		local collisionGroup = "PlayerCharacters"

		local animationTrack = char:FindFirstChild("Humanoid"):LoadAnimation("18888269096")

		PhysicsService:RegisterCollisionGroup(collisionGroup)
		PhysicsService:CollisionGroupSetCollidable(collisionGroup,collisionGroup, false)

		for _, parts in pairs(client.Character:GetChildren()) do
			if parts:IsA("BasePart") then
				parts.CollisionGroup = collisionGroup
			end
		end

		local targetChar = workspace.Rig -- replace with your target

		for _, parts in pairs(targetChar:GetChildren()) do
			if parts:IsA("BasePart") then
				parts.CollisionGroup = collisionGroup
			end
		end

		targetChar:FindFirstChild("HumanoidRootPart").CFrame = client.Character:FindFirstChild("HumanoidRootPart").CFrame * CFrame.new(0,2,-2)

		local weldConstraint = Instance.new("WeldConstraint")
		weldConstraint.Parent = client.Character:FindFirstChild("UpperTorso")
		weldConstraint.Part0 = client.Character:FindFirstChild("UpperTorso")
		weldConstraint.Part1 = targetChar:FindFirstChild("HumanoidRootPart")

		char:FindFirstChild("Humanoid").WalkSpeed = 0
		char:FindFirstChild("Humanoid").JumpPower = 0

		--Play animation

		animationTrack:Play()

		--Listen to when the animation ends and the set the humanoid walkspeed to 16 and jumpPower to 50

		animationTrack.Ended:Connect(function()
			weldConstraint:Destroy()
			PhysicsService:CollisionGroupSetCollidable(collisionGroup,collisionGroup, true)
			char:FindFirstChild("Humanoid").WalkSpeed = 16
			char:FindFirstChild("Humanoid").JumpPower = 50
		end)
	end
end)

In the Local script You have to make a table called data that should look like this:

local data = {
[“Ability”] = 1
}

And then pass it to the remote event
remoteEvent:FireServer(data)

!!! Make sure u have a rig in the workspace called “Rig”. If You want i can i make it so like it takes the closest character to Ur player

Did exactly what you said but now I’m receiving another error that says unable to cast value to object. from line 10 of the server script.
image

No idea what went wrong.

Yeah, forgot to mention because i thought u already knew that but add an animation inside th script and paste Ur animation ID inside the AnimationId. Next, refrence the animation inside the LoadAnimation. Kinda like this:

animationTrack = char:FindFirstChild(“Humanoid”):LoadAnimation(script.Animation)

Lovely, this worked! The script works flawlessly now. So In order to do 2 seperate animations at once, I need to fire a remote event so the client and server can know what to do awesome! Further question, when the animation plays, it works fine but the target rig looks a bit wonky, i’m assuming I have to properly weld it in order to make it look better and as for the target’ rig’s animations, do I just type and write a whole new variable such as animationtrack 2 solely just for the target rig’s animations?

Ummm yeah well because i did not have the animation i could not offset the rig as it should… Later in today imma optimize it a Little. Also i noticed your player’s character fall when the animation ends… Is that intended or You want a quick fix?

Lol, no that was not intended. A quick fix would be nice. I’m trying to have it where I slam my opponent on the ground and my target remains on the ground ragdolled for a few seconds and then get back up. I just need to know how to keep the animations from being wonky as in moon animator, they look fine but when I play them, its probably cause of me not knowing how to properly weld them.

Ok so first, imma do a fix and send it here and secondly can u send me the animation in moon animator to see how it should play?

Here you go, this is what it should look like.