I try to explain the problem as short as possible.
So I have been trying to make a handcuffing/ arrest system like I did a long time ago using welds.
But it’s now that I encountered a problem. Whenever I click on a player (currently a Dummy for quick testing) it puts them infront of me like normal but I am unable to walk forwards and backwards. I can still turn my character and the target turns along with it. However, when I jump and walk forwards/ backwards I move very very slowly in the facing direction.
I’ve tried using a different method with the RunService, however, when I am using .Hearbeat the target lags behind so it’s not really an option. Using .RenderStepped does not work on the server!
(Note that I have the code below only handles the “cuffing” case as of now and not the “realeasing” case)
Here is the Code for the client:
local Player = game.Players.LocalPlayer
local Cuff_Highlight = script:WaitForChild("Cuff_Selection")
local Mouse = Player:GetMouse()
local rfunc_Cuff = game.ReplicatedStorage:WaitForChild("rfunc_Cuff")
local CUFFING_DISTANCE = 8
local PreHighlight = nil
local function RemoveHighlight()
if (PreHighlight) then
PreHighlight:Remove()
PreHighlight = nil
end
end
Mouse.Move:Connect(function()
local Target = Mouse.Target
if (Target == nil) then
RemoveHighlight()
return
end
-- if the mouse houvers over a player within reach, the player's character gets highlighted
if (Target.Parent:FindFirstChild("HumanoidRootPart") --[[and game.Players:FindFirstChild(Target.Parent.Name)]]) then
local Distance:number = (Player.Character:FindFirstChild("HumanoidRootPart").Position - Target.Parent.HumanoidRootPart.Position).magnitude
print(Distance.." studs away from target")
if (Distance < CUFFING_DISTANCE) then
if (PreHighlight == nil) then
PreHighlight = Cuff_Highlight:Clone()
PreHighlight.Parent = Target.Parent
end
else
RemoveHighlight()
end
else
RemoveHighlight()
end
end)
Mouse.Button1Down:Connect(function()
local Target = Mouse.Target
if (Target == nil) then return end
if (Target.Parent:FindFirstChild("HumanoidRootPart") --[[and game.Players:FindFirstChild(Target.Parent.Name)]]) then
local Cuffed = rfunc_Cuff:InvokeServer(Player, Target.Parent.HumanoidRootPart)
if (Cuffed) then
-- Target has been cuffed
end
end
end)
Here is the Code that runs on the Server:
local rfunc_Cuff = game.ReplicatedStorage:WaitForChild("rfunc_Cuff")
local HasCuffed = {}
rfunc_Cuff.OnServerInvoke = function(Player, VerifyPlayer, Target)
-- sanity checks
if (Target == nil) then return false end
if (Player ~= VerifyPlayer) then Player:Kick("Uncaught Exception") end
if (table.find(HasCuffed, Player.Name)) then
return false
end
-- verifying distance
local Distance = (Player.Character:FindFirstChild("HumanoidRootPart").Position-Target.Position).magnitude
if ( Distance < 8) then
-- Handling the cuffing
table.insert(HasCuffed, Player.Name)
local TargetHumanoid = Target.Parent.Humanoid
TargetHumanoid.WalkSpeed = 0
TargetHumanoid.JumpPower = 0
-- editing the Target
for i, part in pairs(Target.Parent:GetChildren()) do
if (part:IsA("BasePart")) then
part.Massless = true
part.CanCollide = false
end
end
-- moving the target 1.75 studs infront of the player
local CFrame = Player.Character.HumanoidRootPart.CFrame
Target.Parent.HumanoidRootPart.CFrame = CFrame + CFrame.LookVector * 1.75
-- weld constraint
local CuffWeld = Instance.new("WeldConstraint")
CuffWeld.Parent = Player.Character.HumanoidRootPart
CuffWeld.Part1 = Target.Parent.HumanoidRootPart
CuffWeld.Part0 = Player.Character.HumanoidRootPart
return true
end
end
Here is a video: