Setting CFrames to true

I was reviewing some old code for a game to get some ideas of how to make an arrest tool similar to what the game had. What I was looking for exactly was how they made the targetted player be “attached” to the police officer when he used the cuff on them.

I know you can use welds and other tricks like looping and readjusting their cFrame but I wanted to see how the game actually did it.

I’ve stumbled across this abomination.

if targetplayer ~= nil and (player.Character.Torso.Position - targetplayer.Character.Torso.Position).Magnitude <= 8 then
targetplayer.Character.Torso.Anchored = true
load(targetplayer.Character.Humanoid, nil)
targetplayer.Character.Torso.Anchored, targetplayer.Character.Torso.CFrame = true, player.Character.Torso.CFrame *,0,-2.5)

The load() function is just an animation function, but I’m wondering what the hell does this mean.

targetplayer.Character.Torso.Anchored, targetplayer.Character.Torso.CFrame = true, player.Character.Torso.CFrame *,0,-2.5)

I’ve never seen any code like this, which why believe maybe because this is an old method that people don’t use anymore, since this game was scripted about 4 years ago, but it still works, you can grab people with the arrest tool.

tl;dr I want to understand what that line is doing and why it works.

EDIT: Due to the code being old, I’ve had a big misunderstanding of how it functioned which is what led me to ask a somewhat dumb question. I won’t withdraw the post since other people can face the same problem.

It’s possible to set multiple variables on a single line.
Like if you did this:

local x, y = 1, 2
print(x) --> 1
print(y) --> 2

The code you found is just setting both the Torso’s Anchored and CFrame properties.


I’m going to explain things line by line to help you understand

For our first line we start with an if statement that checks the targetplayer (I’m guessing a instance) exists and then also checks if the magnitude (distance between two instances) is below 8. Magnitude can be calculated by subtracting one Vector3 from another.

For our second line it anchors the player (I’m guessing to stop movement)

Four our third line we load an animation

Four our fourth line it looks like we set the players cframe to a new position.

1 Like

I understand that you can set multiple variables in a single line but no variables are being set on the line I’m referring to. In addition to that, it’s literally setting the CFrame to true which by itself shouldn’t be possible, at least within my knowledge

That line is what makes the player stick with whoever is holding them.

My only doubt is about the last line, it’s setting a CFrame value to true, and then setting the officer’s torso CFrame to its position but -2.5 studs behind.

But when the officer moves around the player sticks with him, even though there’s no welds, and that’s the line responsible for making that happen. This is what I’m not understanding, how can that line attach the player to the officer who’s holding him?

This line is attaching a player to the officer who’s holding him, even if the officer moves around the player’s position will change to adapt itself to those restrictions. Even though there’s no loops or checks it still works, that’s my doubt.

Only the anchored property is being set to true. The CFrame is being set to a CFrame.

That line is setting a tuple:

targetplayer.Character.Torso.Anchored, targetplayer.Character.Torso.CFrame

To another tuple:

 = true, player.Character.Torso.CFrame *,0,-2.5)

Which is equivalent to:

targetplayer.Character.Torso.Anchored = true
targetplayer.Character.Torso.CFrame = player.Character.Torso.CFrame *,0,-2.5)

It’s not setting Anchored to true, read the line again, it’s setting the CFrame to true, the anchored property is just being read.


From here.

Think of targetplayer.Character.Torso.Anchored as the a variable and targetplayer.Character.Torso.CFrame as the b variable in this example.

1 Like

You can set properties on a single line too. The first property being set is Anchored, which is the first value after the equals (true).

On a side note, it should be affecting HumanoidRootPart, as R15 rigs don’t have a Torso (I understand it’s old).

1 Like

I think I’ve managed to understand what it says now, but it still doesn’t go through my head how’s it looping it if it’s not wrapped in a loop. When you hold someone and move around, their position will readjust accordingly, but there’s no loop that keeps control of that.

This is the only line that affects the player’s cFrame.

I managed to understand now what you mean, I was struggling especially because of how poorly this was written.

I believe I have found what I was looking for, the loop is being ran on the Client Script for some reason? Which is loop firing the remote event that controls the player position. I can’t blame whoever wrote this cause it’s old but it’s not a common method at all nowadays, which made me struggle a lot.

Thanks for helping me out and I’m sorry for being a little hard headed, I’m stressed and this old code is just making it worst