Arrest Tool Help

  1. What do you want to achieve? I want to to reference the Target.Parent even if I didn’t click on a Player.

  2. What is the issue? I don’t know how to reference the Victim without clicking the player. I want to know how to get the Victim when I click anywhere. I want this to be done when the Victim is already arrested. So when I activate the tool, I can release the Victim and then their inventory gets shown again.

  3. What solutions have you tried so far?
    I have tried searching for posts, but nothing really helped so far.

--[[

GOAL: WHEN YOU CLICK SOMEONE YOU CAN DETAIN THEM. WHEN YOU CLICK ANYWHERE
YOU CAN RELEASE THEM.

]]

local tool = script.Parent
local LocalPlayer = game.Players.LocalPlayer
local arrestEvent = game.ReplicatedStorage.Arrest
local releaseEvent = game.ReplicatedStorage.Release
local someonearrested = false

tool.Activated:Connect(function()
	local Victim = LocalPlayer.GetMouse().Target
	if Victim then
		if LocalPlayer.Character and Victim.Character then
			if someonearrested == false then
				arrestEvent:FireServer(Victim)
			else
				releaseEvent:FireServer()
			end
		end
	end
end)
1 Like

if you want to reference the target’s parent, even if you didn’t click on a player, and release the arrested victim when activating the tool, you can do this by modifying the script as follows:

  1. Track the arrested players using a table.
  2. When you activate the tool, check if the target’s parent is in the arrested players’ table.
  3. If the target’s parent is in the arrested players’ table, release them and show their inventory.

Here’s an example of how you can modify your script:

local tool = script.Parent
local LocalPlayer = game.Players.LocalPlayer
local arrestEvent = game.ReplicatedStorage.Arrest
local releaseEvent = game.ReplicatedStorage.Release
local arrestedPlayers = {}

tool.Activated:Connect(function()
    local target = LocalPlayer:GetMouse().Target
    if target then
        local possibleVictim = target.Parent
        local victimPlayer = game.Players:GetPlayerFromCharacter(possibleVictim)

        if victimPlayer then
            if not arrestedPlayers[victimPlayer.UserId] then
                arrestEvent:FireServer(victimPlayer)
                arrestedPlayers[victimPlayer.UserId] = true
            else
                releaseEvent:FireServer(victimPlayer)
                arrestedPlayers[victimPlayer.UserId] = nil
                -- Your logic to show the victim's inventory again
            end
        end
    end
end)

This script creates a table called arrestedPlayers to keep track of the arrest status for each player. When the tool is activated, it checks if the target’s parent is in the arrested players’ table, and if so, it releases the victim and shows their inventory again. If the target’s parent is not in the arrested players’ table, it arrests the player and adds them to the table.

2 Likes

Hi, after using this code, it still seems to only release the player when I still click the player.

I tried fixing it, here is my code:

local tool = script.Parent
local LocalPlayer = game.Players.LocalPlayer
local arrestEvent = game.ReplicatedStorage.Arrest
local releaseEvent = game.ReplicatedStorage.Release
local arrestedPlayers = {}

tool.Activated:Connect(function()
	local Victim = LocalPlayer:GetMouse().Target
	if Victim then
		local possibleVictim = Victim.Parent
		local victimPlayer = game.Players:GetPlayerFromCharacter(possibleVictim)
		if victimPlayer then
			if not arrestedPlayers[victimPlayer.UserId] then
				arrestEvent:FireServer(victimPlayer)
				arrestedPlayers[victimPlayer.UserId] = true
			else
				releaseEvent:FireServer(victimPlayer)
				arrestedPlayers[victimPlayer.UserId] = nil
			end
		else 
			if not arrestedPlayers[victimPlayer.UserId] then
				arrestEvent:FireServer(victimPlayer)
				arrestedPlayers[victimPlayer.UserId] = true
			else
				releaseEvent:FireServer(victimPlayer)
				arrestedPlayers[victimPlayer.UserId] = nil
			end
		end
	end
end)

error: Players.Player4.Backpack.Tool that arrests people.LocalScript:28: attempt to index nil with ‘UserId’ - Client - LocalScript:28

1 Like