Quick actions

cmd+k|ctrl+k

Navigation

Languages

hold character [tool]

Snippet info

Language

Lua

Visibility

public

Author

lazarovalkyrie

Created

2024-05-19T19:06:28.883023Z

Updated

2024-08-25T23:21:08.714724Z

local plr = owner
local char = plr.Character
local hum = char:FindFirstChildOfClass("Humanoid")
local root = char:FindFirstChild("HumanoidRootPart") or char:FindFirstChild("Torso") or char:FindFirstChild("UpperTorso")
local bp = plr.Backpack

local tool = Instance.new("Tool")
tool.Name = 'Hold Character'
tool.ToolTip = 'Click with arm on someone to capture!'
tool.CanBeDropped = false
tool.Parent = bp

local h = Instance.new("Part")
h.Name = 'Handle'
h.Size = Vector3.new(2,6,2)
h.Transparency = 1
h.Anchored = false
h.Massless = true
h.CanCollide = false
h.Parent = tool

local network_loop,char_loop,captured

function is_captured()
	if captured and captured[1] and captured[2] and captured[3] and captured[2].Parent then
		return true
	end
end

function release()
    if captured then else
    	tool.Name = 'Hold [N/A]'
	    captured = nil
	    return
    end

	if captured[1] then
		captured[1]:Destroy()
	end

	if captured[3] then
		captured[3].PlatformStand = false
	end
	
	local nl = network_loop ~= nil
	
	if network_loop then
		network_loop:Disconnect()
		network_loop = nil
	end
	if char_loop then
		char_loop:Disconnect()
		char_loop = nil
	end

	if captured[2] and captured[2].Parent then
		for i,v in pairs(captured[2]:GetDescendants()) do
			if v:IsA("BasePart") then
				if nl and v.Parent == captured[2] then
					v:SetNetworkOwnershipAuto()
				end
				v.CollisionGroupId = 0
				v.Massless = false
			end
		end
	end
	
	tool.Name = 'Hold [N/A]'
	captured = nil
end

function perish()
	if is_captured() then else return end
end

function throw()
	if is_captured() then else return end
	local tchar = captured[2]
	local troot = tchar:FindFirstChild("HumanoidRootPart") or tchar:FindFirstChild("Torso") or tchar:FindFirstChild("UpperTorso")
	
	release()
	
	local bv = Instance.new("BodyVelocity")
	bv.MaxForce = Vector3.new(1/0,1/0,1/0)
	bv.Velocity = root.CFrame.LookVector*100+Vector3.new(0,7,0)
	bv.Parent = troot
	game:GetService("Debris"):AddItem(bv,0.2)
end

function unequip(thum)
    --thum:UnequipTools()
end

tool.Unequipped:Connect(function()
	release()
end)

local r = Instance.new("RemoteEvent")
r.Parent = char

r.OnServerEvent:Connect(function(lplr,key)
	if lplr == plr then else return end
	if key == Enum.KeyCode.Q then
		perish()
	elseif key == Enum.KeyCode.E then
		throw()
	end
end)

NLS([[
local plr = game:GetService("Players").LocalPlayer
local r = script.Parent
local uis = game:GetService("UserInputService")

uis.InputBegan:Connect(function(input,gp)
	if gp then return end
	if input.UserInputType == Enum.UserInputType.Keyboard then
		local key = input.KeyCode
		r:FireServer(key)
	end
end)
]],r)

tool.Activated:Connect(function()
    if hum and hum.PlatformStand then
        return
    end
    
	local olp = OverlapParams.new()
	olp.MaxParts = 0
	olp.FilterType = Enum.RaycastFilterType.Exclude
	olp.FilterDescendantsInstances = {char,tool}

	if captured then
		release()

		return
	end

	local p = workspace:GetPartBoundsInBox(h.CFrame,h.Size,olp)
	for i,v in ipairs(p) do
		local tchar = v:FindFirstAncestorOfClass("Model")
		if tchar then else continue end
		local tplr = game:GetService("Players"):GetPlayerFromCharacter(tchar)
		local thum = tchar:FindFirstChildOfClass("Humanoid")
		local troot = tchar:FindFirstChild("HumanoidRootPart") or tchar:FindFirstChild("Torso") or tchar:FindFirstChild("UpperTorso")
		if thum and thum.Health > 0 and troot then else continue end

		thum:ChangeState(Enum.HumanoidStateType.GettingUp)
		thum.PlatformStand = true

		local weld = Instance.new("Weld")
		weld.Part0 = h
		weld.Part1 = troot
		weld.Parent = h
				
		for i,v in ipairs(tchar:GetDescendants()) do
			if v:IsA("BasePart") then
				if v.Parent == tchar then
					v:SetNetworkOwner(plr)
				end
				v.Massless = true
			end
		end

		if tplr then
			local tbp = tplr:FindFirstChildOfClass("Backpack")

			for i,o in pairs(tchar:GetChildren()) do
				if o:IsA("Tool") then
					unequip(thum)
				end
			end

			char_loop = tchar.ChildAdded:Connect(function(o)
				if o:IsA("Tool") then
					unequip(thum)
				end
			end)

			network_loop = game:GetService("RunService").Heartbeat:Connect(function()
				if tchar and tchar.Parent and plr and weld and weld.Parent and thum and thum.PlatformStand then else
					char_loop:Disconnect()
					network_loop:Disconnect()
					network_loop = nil
					char_loop = nil
					
					release()
					return
				end

				unequip(thum)
			end)
		end

		tool.Name = tchar.Name
		captured = {weld,tchar,thum}

		break
	end
end)
INFO