Need assistance with detain tool

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? We want the detain tool to be able to detain people without any problems

  2. What is the issue?

  • Detain tool has a far reach to it were it detains anyone from anywhere if you click on the player with the detain tool. We’re trying to limit how far you can detain someone.

  • The detain tool breaks all the seats in our game. What I mean by that is if someone is sitting down and they get detained the seats move with them. And we have invisible seats everywhere.

Proof

https://gyazo.com/40b7887eb750518d1d39a3cef7b3c9f1
https://gyazo.com/2eb3d0551f3c4616f4a23e29686cdcf8

  1. What solutions have you tried so far? We tried adding in a jump script into it but it didn’t work!
Scripts

DetainHandler script

-- local Players = game:GetService("Players")
local tool = script.Parent
local event = tool.DetainEvent
local detaining = nil

local function detain(plr, targetPlr)
	if targetPlr and targetPlr.Character and targetPlr.Character.Parent then
		local hrp = targetPlr.Character:WaitForChild("HumanoidRootPart")
		local hum = targetPlr.Character.Humanoid
		if hum.Sit then targetPlr.Character.Humanoid.Jump = true end
		spawn(function()
			local conn
			conn = plr.CharacterRemoving:Connect(function()
				hrp.Anchored = false
				detaining = nil
				conn:Disconnect()
			end)
			while detaining == targetPlr do
				if targetPlr and targetPlr.Character and targetPlr.Character.Parent then
					hrp = targetPlr.Character:WaitForChild("HumanoidRootPart")
					local tool = targetPlr.Character:FindFirstChildWhichIsA("Tool")
					if tool then
						tool.Parent = targetPlr.Backpack
					end
					if plr and plr.Character and plr.Character.Parent then
						if hum.Sit then targetPlr.Character.Humanoid.Jump = true end
						hrp.CFrame = plr.Character:WaitForChild("HumanoidRootPart").CFrame * CFrame.new(0,0,-10)
					else
						break
					end
				end
				wait()
			end
		end)
	end
end

event.OnServerEvent:Connect(function(plr, targetPlr)
	if tool.Parent == plr.Character or tool.Parent == plr.Backpack then
		if detaining == targetPlr then
			detaining = nil
		else
			detain(plr, targetPlr)
			detaining = targetPlr
		end
	else
		plr:Kick("20 iq move")
	end
end)

DetainLocal script

local Players = game:GetService("Players")
local LP = Players.LocalPlayer
local mouse = LP:GetMouse()
local tool = script.Parent
local event = tool:WaitForChild("DetainEvent")
local detaining = false

local function isAChar(target)
	for i,v in next, Players:GetPlayers() do
		if v.Character and target:IsDescendantOf(v.Character) then
			return v
		end
	end
	return nil
end

tool.Activated:Connect(function()
	local target = mouse.Target
	if target then
		local targetPlr = isAChar(target)
		if targetPlr and targetPlr ~= LP then
			event:FireServer(targetPlr)
			detaining = true
			return
		end
	end
	if detaining then
		event:FireServer()
		detaining = false
	end
end)
1 Like

Check the magnitude between the player and the target first, if it’s within x distance then continue with your script. In regard to breaking seats, I’d probably prefer to rely on the “SeatPart” than to force the humanoid to jump e.g. find the SeatPart, then of that find the child “SeatWeld” then break that joint.

2 Likes

If you want to add a maximum range, you can use the function Player:GetDistanceFromCharacter. This is an in-built alternative to (pos1 - pos2).magnitude.

2 Likes