Handcuffs system - Help me make it more efficient!

Alright, so I have made a very simple handcuff system, basically teleporting a player to the prison 5 seconds after they are arrested. It works, but it is not the most efficient way. Any advice on how to make it cleaner?



game.Players.PlayerAdded:Connect(function(plr)
	
	local bad = Instance.new("BoolValue")
	bad.Name = "Bad"
	bad.Parent = plr
	bad.Value = false

game.Workspace.Part.Touched:Connect(function(hit)
		local plrHit = game.Players:FindFirstChild(hit.Parent.Name)
		if plrHit.Team == game.Teams.Prisoner then
			plrHit.Team = game.Teams.Criminal
			bad.Value = true
		game.ReplicatedStorage.RemoteEvent:FireClient(plrHit)
	end
end)

local char = plr.CharacterAdded:Wait()
local HRoot = char.HumanoidRootPart
local pSpawn = workspace.Prisons:GetChildren()
local radPrison = pSpawn[math.random(1,#pSpawn)]


HRoot.Touched:Connect(function(h)
	if h.Parent.Name == "Handcuffs" then
		if plr:FindFirstChild("Bad") then
		if plr.Bad.Value == true then
		if plr.Team == game.Teams.Criminal then
		char.Humanoid.WalkSpeed = 0
		wait(5)
		plr.Team = game.Teams.Prisoner
		plr.Bad.Value = false
		char.HumanoidRootPart.Position = radPrison.Position
		char.Humanoid.WalkSpeed = 16
		
		local newPrison = game.Workspace:FindFirstChild(radPrison.Name)
		if newPrison:FindFirstChild("InJail") then
			if newPrison.InJail.Value == false then
				newPrison.InJail.Value = true
				
				wait(10)
				
				newPrison.InJail.Value = false
				local part = newPrison.Wall1
				local goal = {}
				goal.Position = newPrison.Wall1.Position + Vector3.new(0,0, 20)
 
				local tweenInfo = TweenInfo.new(1)
 
				local tween = TweenService:Create(part, tweenInfo, goal)
 
				tween:Play()
			end
		end
		end
	end
	end
	end
end)

end)
4 Likes

me: sees this post
also me: hold my beer

This post inspired me to create my own.

Here’s the game if you want to take a look at how I implemented my own and all the improvements I did in your current one. (Make sure to play with me if you intend to. I’m waiting on the reply. Besides, you can’t arrest yourself)
Basically, instead of having a bool, I made teams. I made a localscript inside a handcuff tool. Here’s how it works:

  1. Once equipped, it plays a free audio I put in. (not necessary here)
  2. Once activated, it fires a ray in front (lookvector of the HRT) and has a distance variable to determine how far it fires the ray.
  3. If the player in front is a criminal, (you can also tweak it to detect if the player’s facing away from you, because that’s how most police officers arrest people anyways), fire a remote with target player as a parameter.
  4. The server picks up the remote signal and confirms if both this players are in correct teams, arrest the player; that is, anchors the player, put a UI on the player’s head and countdown from 0 to 5.

To get my source, you can download the game from the link I put up above.

Anyways, this ain’t about me, ain’t it?

What should be improved

1. I don’t really recommend doing all this from the server. It’s fine, but not efficient

2. I don’t really think that the touching event on the handcuff is a good method as exploiters can easily exploit that, since the player can just teleport to every player arresting them. With my method, you can easily detect if the player is spamming or set a cooldown to annoy the hacker more. On top of this, you can make a neat anti-teleport script.

3. Instead of doing the work of choosing a prison spawn yourself, let Roblox do it for you. In the link I put up, I made spawn locations with the criminal team teamcolor and they’re all auto-assignable. I used the concept of
If multiple eligible spawns are available to a Player, a random one will be chosen as shown in the SpawnLocation wiki
. We can agree that Roblox can do a really good job of randomizing than the math.random() function.

The rest are alright as they are.

1 Like

Roblox agrees with you on the math.random aspect - read this aptly named announcement: A Random Feature

1 Like

I tested your scripts out, and have to say, it is really efficient! Nice one! It taught me a lot, so I really appreciate it!