Reset data after arrest

Hey. I have a script where a player can be arrested with a set amount of time and allat works fine but there is a problem. Because whenever the player gets arrested I obviously want the arresting officers data to get reset so he can arrest other people (specifically the arrestTime) but resetting it interferes with the current arrest.

rep.JailStuff.RemoteEvents.Arrested.OnServerEvent:Connect(function(plr, arrestingOfficer)
	local cuffedPlayer = game.Players[plr.PlayerGui.Interface.ArrestFrame.PlayerInCuffs.Value]
	local ArrestTime = plr.PlayerGui.Interface.ArrestFrame.ArrestTime
	
	local previousTeam = cuffedPlayer.Team
	
	cuffedPlayer.Team = game.Teams.Jailed
	cuffedPlayer.JailInfo.isArrested.Value = true
	cuffedPlayer.JailInfo.timeLeft.Value = ArrestTime.Value
	
	game.Workspace[arrestingOfficer]:FindFirstChild("Cuffs").Parent = game.Players[arrestingOfficer].Backpack
	cuffedPlayer.PlayerGui.Interface.TimeLeftInJail.Visible = true
	
	workspace[cuffedPlayer.Name]:MoveTo(workspace.SpawnLocations.JailedSpawn.Position)
	wait(.5)
	workspace[cuffedPlayer.Name].HumanoidRootPart.Anchored = true
	
	local timeLeftText = cuffedPlayer.PlayerGui.Interface.TimeLeftInJail.TimeLeft
	timeLeftText.Text = "Time Left: "..ArrestTime.Value
	
	plr.PlayerGui.Interface.ArrestFrame.ArrestTime.Value = 0
	plr.PlayerGui.Interface.ArrestFrame.PlayerInCuffs.Value = nil
	plr.PlayerGui.Interface.ArrestFrame.CuffedPlayer.Text = nil
	
	for i = ArrestTime.Value, 0, -1 do
		timeLeftText.Text = "Time Left: " .. tostring(i)
		wait(1)
	end
	
	cuffedPlayer.JailInfo.isArrested.Value = false
	cuffedPlayer.JailInfo.timeLeft.Value = 0
	cuffedPlayer.Team = previousTeam
	workspace[cuffedPlayer.Name].HumanoidRootPart.Anchored = false
	game.Workspace[cuffedPlayer.Name].Humanoid:TakeDamage(100)
	cuffedPlayer.PlayerGui.Interface.TimeLeftInJail.Visible = false
end)

How do i make this work?

I’ve figured it out.

Updated code:

rep.JailStuff.RemoteEvents.Arrested.OnServerEvent:Connect(function(plr, arrestingOfficer)
	local cuffedPlayer = game.Players[plr.PlayerGui.Interface.ArrestFrame.PlayerInCuffs.Value]
	local ArrestTime = plr.PlayerGui.Interface.ArrestFrame.ArrestTime.Value

	-- Setting up the cuffed player
	local previousTeam = cuffedPlayer.Team

	cuffedPlayer.Team = game.Teams.Jailed
	cuffedPlayer.JailInfo.isArrested.Value = true
	cuffedPlayer.JailInfo.timeLeft.Value = ArrestTime

	-- Move the cuffs to the officer's backpack
	game.Workspace[arrestingOfficer]:FindFirstChild("Cuffs").Parent = game.Players[arrestingOfficer].Backpack
	cuffedPlayer.PlayerGui.Interface.TimeLeftInJail.Visible = true

	-- Move the cuffed player to the jail spawn
	workspace[cuffedPlayer.Name]:MoveTo(workspace.SpawnLocations.JailedSpawn.Position)
	wait(2)
	workspace[cuffedPlayer.Name].HumanoidRootPart.Anchored = true

	-- Display the remaining time in jail
	local timeLeftText = cuffedPlayer.PlayerGui.Interface.TimeLeftInJail.TimeLeft
	timeLeftText.Text = "Time Left: " .. ArrestTime

	-- Create a coroutine to handle the countdown
	coroutine.wrap(function()
		for i = ArrestTime, 0, -1 do
			timeLeftText.Text = "Time Left: " .. tostring(i)
			wait(1)
		end

		-- After countdown ends, reset the cuffed player
		cuffedPlayer.JailInfo.isArrested.Value = false
		cuffedPlayer.JailInfo.timeLeft.Value = 0
		cuffedPlayer.Team = previousTeam
		workspace[cuffedPlayer.Name].HumanoidRootPart.Anchored = false
		game.Workspace[cuffedPlayer.Name].Humanoid:TakeDamage(100)
		cuffedPlayer.PlayerGui.Interface.TimeLeftInJail.Visible = false
	end)()

	-- Reset the officer's arrest data immediately so they can arrest againso 
	plr.PlayerGui.Interface.ArrestFrame.ArrestTime.Value = 0
	plr.PlayerGui.Interface.ArrestFrame.PlayerInCuffs.Value = nil
	plr.PlayerGui.Interface.ArrestFrame.CuffedPlayer.Text = nil
end)

yes i used chatgpt :rofl:

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.