Handcuff range issue

So basically im a builder and im making a prison game, basically i need handcuffs but most of them don’t really have a range and can basically handcuff anyone from any distance and ofcourse that is an issue.
Here are the scripts, credits to Matthew_Plays55 for this cause these are his handcuffs and not mine.

Script :

local RemoteEvent = script.Parent:WaitForChild("RemoteEvent")
local TargetTorso = nil
local Using       = false


function CheckForTargetTorso(part)
	if part.Parent:IsA'Model' and part.Parent:FindFirstChildOfClass'Humanoid' then
		local char = part.Parent
		local torso
		if char:FindFirstChild'Torso' then
			torso = char.Torso
		elseif char:FindFirstChild'UpperTorso' then
			torso = char.UpperTorso
		end
		return char, torso
	end
end

	
	
function CheckForTorso(player)
	local Char = player.Character
	if Char:FindFirstChild'Torso' then
		return Char.Torso
	else 
		return Char.UpperTorso
	end
end


RemoteEvent.OnServerEvent:Connect(function(player, state, part)
	
	if state == 'Cuff' then
		local target      = nil
		local playerTorso = nil
		
		target, TargetTorso = CheckForTargetTorso(part)
		playerTorso = CheckForTorso(player)
		
		if target then
			Using = true
			RemoteEvent:FireClient(player, 'Use')
			spawn(function()
				if Using then
					TargetTorso.Anchored = true			
				end				
				while Using and wait() do			
					TargetTorso.CFrame = playerTorso.CFrame * CFrame.new( 0, 0, -5 )
				end				
				
			end)			
			
		end
	
	elseif state == 'UnCuff'then	
		Using = false		
		TargetTorso.Anchored = false
		wait()
	end
	
end)

local script :

local Player = game.Players.LocalPlayer
local Mouse  = Player:GetMouse()
local Tool   = script.Parent
local Using  = false
local RemoteEvent = script.Parent:WaitForChild("RemoteEvent")

Tool.Activated:Connect(function()
	if not Using then
		if Mouse.Target then
			RemoteEvent:FireServer('Cuff', Mouse.Target)
		end
	else 
		Using = false
		RemoteEvent:FireServer('UnCuff')
	end
end)

RemoteEvent.OnClientEvent:Connect(function( state )
	if state == 'Use' then
		Using = true
	end
end)

If anyone knows how to add a specific range to these handcuffs, please reply, im not a scripter by any chance since im mostly into building and modelling.

1 Like

You can use Player:DistanceFromCharacter(Mouse.Hit.p which will return how many studs you are away from the mouse hit position. You can then add a maximum range of which player can handcuff a prisoner.

Edit: Changed Mouse.Hit.Position to Mouse.Hit.p.

I’ll try it out to see if it works, might aswell edit some of the lines and add a few effects to it like animations and such. Thanks for the reply, really appreciate it!

1 Like

My bad I made a mistake, it should be Mouse.Hit.p not Mouse.Hit.Position

So to add the range, do i need to make a variable or a value ? sorry im not a scripter, mostly a builder.

You could try something like

local Player = game.Players.LocalPlayer or game.Players.PlayerAdded:Wait()

local Distance = Player:DistanceFromCharacter(Mouse.Hit.p)
local Range = 2

if Distance <= Range then
    -- Handcuff
end

Edit: You’d put the check distance part inside the Tool.Activated.

The easiest way to fix this would be to add an if statement that checks the distance between the two characters. For example:

local TargetTorso = nil
local Using = false
local Torso = nil

function CheckForTargetTorso(part)
 if part.Parent:IsA'Model' and part.Parent:FindFirstChildOfClass'Humanoid' then
  local char = part.Parent
  local torso
  if char:FindFirstChild'Torso' then
   torso = char.Torso
  elseif char:FindFirstChild'UpperTorso' then
   torso = char.UpperTorso
  end
  return char, torso
 end
end

 
 
function CheckForTorso(player)
 local Char = player.Character
 if Char:FindFirstChild'Torso' then
  return Char.Torso
 else 
  return Char.UpperTorso
 end
end


RemoteEvent.OnServerEvent:Connect(function(player, state, part)
 
 if state == 'Cuff' then
  local target = nil
  local playerTorso = nil
  
  target, TargetTorso = CheckForTargetTorso(part)
  playerTorso = CheckForTorso(player)
  
  if target then
   Using = true
   RemoteEvent:FireClient(player, 'Use')
   spawn(function()
    if Using then
     TargetTorso.Anchored = true   
    end    
    while Using and wait() do   
     TargetTorso.CFrame = playerTorso.CFrame * CFrame.new( 0, 0, -5 )
    end    
    
   end)   
   
  end
 
 elseif state == 'UnCuff'then 
  Using = false  
  TargetTorso.Anchored = false
  wait()
 end
 
end)