I need help with a jail system because the other one was not answered

I tried making a jail system and everyhting works except i cant get the timer to work properly.

SEVERSCRIPT:


local ReplicatedStorage = game:GetService("ReplicatedStorage")

local NotificationClient = require(ReplicatedStorage:WaitForChild("NotificationClient"))
-- Variable to keep track of occupied cells
local occupiedCells = {} -- A table to store occupied cell names

-- Function to create a button for a player
local function createPlayerButton(player)
	local playerButton = Instance.new("TextButton")´´´

	´´´-- Properties:
	playerButton.Name = player.Name
	playerButton.Parent = script.Parent.Parent.Display
	playerButton.BackgroundColor3 = Color3.fromRGB(48, 48, 48)
	playerButton.BorderColor3 = Color3.fromRGB(255, 255, 255)
	playerButton.Size = UDim2.new(1, 0,0.086, 0)
	playerButton.Font = Enum.Font.Jura
	playerButton.Text = player.Name
	playerButton.TextColor3 = Color3.fromRGB(255, 255, 255)
	playerButton.TextScaled = true
	playerButton.TextSize = 14.000
	playerButton.TextStrokeTransparency = 0.000
	playerButton.TextWrapped = true
	playerButton.AutoButtonColor = false  -- Prevents the button from changing color when clicked

	-- Selection state
	local isSelected = false

	-- Toggle selection when clicked
	playerButton.MouseButton1Click:Connect(function()
		isSelected = not isSelected

		-- Toggle the selection based on the current state
		if isSelected then
			playerButton.BackgroundColor3 = Color3.fromRGB(0, 255, 0)  -- Change the background color when selected
			NotificationClient.Notify("LOCAL NOTIFICATION", playerButton.Name.." Has Been Selected!", 3)
		else
			playerButton.BackgroundColor3 = Color3.fromRGB(48, 48, 48) -- Reset the background color when unselected
			NotificationClient.Notify("LOCAL NOTIFICATION", playerButton.Name.." Has Been UnSelected!", 3)
		end

		-- Unselect all other buttons unless the clicked button is already selected
		for _, child in ipairs(script.Parent.Parent.Display:GetChildren()) do
			if child:IsA("TextButton") and child ~= playerButton then
				if isSelected then
					child.BackgroundColor3 = Color3.fromRGB(48, 48, 48) -- Reset the background color of unselected buttons
				end
			end
		end
	end)
end

-- Function to update the player list
local function updatePlayerList()
	-- Clear the existing list
	for _, child in ipairs(script.Parent.Parent.Display:GetChildren()) do
		if child:IsA("TextButton") then
			child:Destroy()
		end
	end

	-- Create buttons for all players
	for _, player in ipairs(game.Players:GetPlayers()) do
		createPlayerButton(player)
	end
end

local function teleportPlayer(selectedPlayer, position)
	local character = selectedPlayer.Character
	if character then
		local humanoidRootPart = character:FindFirstChild("HumanoidRootPart")
		if humanoidRootPart and humanoidRootPart:IsA("BasePart") then
			humanoidRootPart.CFrame = CFrame.new(position)
		end
	end
end

-- Function to arrest the selected player with a timer
local function arrestPlayer(selectedPlayer, hours, minutes, seconds)
	-- Check if the selected cell is occupied
	local playerName = selectedPlayer.Name
	ReplicatedStorage.NotifyArrested:FireServer(selectedPlayer)
	if occupiedCells[playerName] then
		warn("Cell is already occupied by another player.")
		NotificationClient.Notify("LOCAL NOTIFICATION", "Cell is already occupied by another player.", 3)
		return
	end

	-- Handle empty textboxes and default them to 0
	hours = tonumber(hours) or 0
	minutes = tonumber(minutes) or 0
	seconds = tonumber(seconds) or 0

	-- Check if valid time values are entered
	if hours < 0 or minutes < 0 or seconds < 0 then
		warn("Invalid time values.")
		NotificationClient.Notify("LOCAL NOTIFICATION", "Invalid time values.", 3)
		return
	end

	-- Calculate the total time in seconds
	local totalTime = hours * 3600 + minutes * 60 + seconds
	

	teleportPlayer(selectedPlayer, Vector3.new(8.12, 3.202, -54.586))
	TweenService:Create(game.Workspace.JailCell.JailBars:FindFirstChild("JailDoor"), TweenInfo.new(0.5, Enum.EasingStyle.Sine, Enum.EasingDirection.Out), {Position = Vector3.new(7.141, 3.82, -61.01)}):Play()

	-- Find the TimeSign in Workspace
	

	-- After arresting the player, mark the cell as occupied
	occupiedCells[playerName] = true

	-- Remove the occupied status after the timer ends (optional)
	wait(hours * 3600 + minutes * 60 + seconds)
	occupiedCells[playerName] = nil
end

-- Create the "Arrest" button
local arrestButton = Instance.new("TextButton")
arrestButton.Name = "ArrestButton"
arrestButton.Parent = script.Parent.Parent.Parent
arrestButton.BackgroundColor3 = Color3.fromRGB(255, 0, 0)
arrestButton.BackgroundTransparency = 0.5
arrestButton.BorderColor3 = Color3.fromRGB(255, 255, 255)
arrestButton.Size = UDim2.new(0.193, 0,0.06, 0)
arrestButton.Position = UDim2.new(0.686, 0,0.168, 0)
arrestButton.Font = Enum.Font.Jura
arrestButton.Text = "Arrest"
arrestButton.TextColor3 = Color3.fromRGB(255, 255, 255)
arrestButton.TextScaled = true
arrestButton.TextSize = 14.000
arrestButton.TextStrokeTransparency = 0.000
arrestButton.TextWrapped = true

-- Create TextBoxes for inputting timer duration
local hourInput = Instance.new("TextBox")
hourInput.Name = "HourInput"
hourInput.Parent = script.Parent.Parent.Parent
hourInput.BackgroundColor3 = Color3.fromRGB(111, 111, 111)
hourInput.BackgroundTransparency = 0.5
hourInput.BorderColor3 = Color3.fromRGB(0, 0, 0)
hourInput.Size = UDim2.new(0.097, 0,0.06, 0)
hourInput.Position = UDim2.new(0.602, 0,0.25, 0)
hourInput.Font = Enum.Font.Jura
hourInput.PlaceholderText = "Hours"
hourInput.Text = "Hours"
hourInput.TextColor3 = Color3.fromRGB(255, 255, 255)
hourInput.TextScaled = true
hourInput.TextSize = 14.000
hourInput.TextWrapped = true

local minuteInput = Instance.new("TextBox")
minuteInput.Name = "MinuteInput"
minuteInput.Parent = script.Parent.Parent.Parent
minuteInput.BackgroundColor3 = Color3.fromRGB(111, 111, 111)
minuteInput.BackgroundTransparency = 0.5
minuteInput.BorderColor3 = Color3.fromRGB(0, 0, 0)
minuteInput.Size = UDim2.new(0.097, 0,0.06, 0)
minuteInput.Position = UDim2.new(0.735, 0,0.25, 0)
minuteInput.Font = Enum.Font.Jura
minuteInput.PlaceholderText = "Minutes"
minuteInput.Text = "Minutes"
minuteInput.TextColor3 = Color3.fromRGB(255, 255, 255)
minuteInput.TextScaled = true
minuteInput.TextSize = 14.000
minuteInput.TextWrapped = true

local secondInput = Instance.new("TextBox")
secondInput.Name = "SecondInput"
secondInput.Parent = script.Parent.Parent.Parent
secondInput.BackgroundColor3 = Color3.fromRGB(111, 111, 111)
secondInput.BackgroundTransparency = 0.5
secondInput.BorderColor3 = Color3.fromRGB(0, 0, 0)
secondInput.Size = UDim2.new(0.097, 0,0.06, 0)
secondInput.Position = UDim2.new(0.869, 0,0.25, 0)
secondInput.Font = Enum.Font.Jura
secondInput.PlaceholderText = "Seconds"
secondInput.Text = "Seconds"
secondInput.TextColor3 = Color3.fromRGB(255, 255, 255)
secondInput.TextScaled = true
secondInput.TextSize = 14.000
secondInput.TextWrapped = true

--localscript
-- Connect the "Arrest" button click event
arrestButton.MouseButton1Click:Connect(function()
	local selectedPlayer = nil
	local textHours = script.Parent.Parent.Parent.HourInput.Text
	local textMinutes = script.Parent.Parent.Parent.MinuteInput.Text
	local textSeconds = script.Parent.Parent.Parent.SecondInput.Text

	-- Find the selected player
	for _, child in ipairs(script.Parent.Parent.Display:GetChildren()) do
		if child:IsA("TextButton") and child.BackgroundColor3 == Color3.fromRGB(0, 255, 0) then
			selectedPlayer = game.Players:FindFirstChild(child.Name)
			break
		end
	end

	-- Check if a player is selected and valid time values are entered
	if selectedPlayer and tonumber(textHours) and tonumber(textMinutes) and tonumber(textSeconds) then
		local hours = tonumber(textHours)
		local minutes = tonumber(textMinutes)
		local seconds = tonumber(textSeconds)
		local playerId = selectedPlayer.UserId

		-- Send a remote request to the server
		game.ReplicatedStorage.UpdateTimerEvent:FireServer({ playerId = playerId, hours = hours, minutes = minutes, seconds = seconds })
	else
		warn("Invalid player selection or time values.")
	end
end)

-- Initial player list update
updatePlayerList()

-- Connect the PlayerAdded and PlayerRemoving events
game.Players.PlayerAdded:Connect(function(player)
	createPlayerButton(player)
end)

game.Players.PlayerRemoving:Connect(function(player)
	local buttonToRemove = script.Parent.Parent.Display:FindFirstChild(player.Name)
	if buttonToRemove then
		buttonToRemove:Destroy()
	end
	-- Release the cell when a player leaves the game
	occupiedCells[player.Name] = nil
end)

-- Search button click event
script.Parent.MouseButton1Click:Connect(function()
	local Text = script.Parent.Parent.Text

	-- Update the player list based on the search criteria
	updatePlayerList()

	-- Filter the list based on the search criteria
	for _, child in ipairs(script.Parent.Parent.Display:GetChildren()) do
		if child:IsA("TextButton") and not string.find(child.Text:lower(), Text:lower()) then
			child:Destroy()
		end
	end
end)```
local script
```local ReplicatedStorage = game:GetService("ReplicatedStorage")
local TweenService = game:GetService("TweenService")

local RemoteEvent = Instance.new("RemoteEvent")
RemoteEvent.Name = "RemoteEventName"
RemoteEvent.Parent = ReplicatedStorage

local occupiedCells = {}

local RemoteEvent = ReplicatedStorage:WaitForChild("UpdateTimerEvent")

local function updateTimerDisplay(data)
	local playerId = data.playerId
	local hours = tonumber(data.hours) or 0
	local minutes = tonumber(data.minutes) or 0
	local seconds = tonumber(data.seconds) or 0

	local player = game.Players:GetPlayerByUserId(playerId)
	if player then
	
	local totalTime = hours * 3600 + minutes * 60 + seconds
	-- Update the time sign with the received timerValue
	local timeSign = workspace:FindFirstChild("TimeSign")
	if timeSign then
		local surfaceGui = timeSign:FindFirstChild("SurfaceGui")
		if surfaceGui and surfaceGui:IsA("SurfaceGui") then
			local textLabel = surfaceGui:FindFirstChild("TimerTextLabel")
			if textLabel and textLabel:IsA("TextLabel") then
				local timerValue = totalTime
				print("timerValue:", timerValue)
				textLabel.Text = string.format("%02d:%02d:%02d", math.floor(timerValue / 3600), math.floor(timerValue / 60 % 60), timerValue % 60)		
				end
			end
		end
	end
end

RemoteEvent.OnServerEvent:Connect(updateTimerDisplay)

local function teleportPlayer(player, position)
	local character = player.Character
	if character then
		local humanoidRootPart = character:FindFirstChild("HumanoidRootPart")
		if humanoidRootPart and humanoidRootPart:IsA("BasePart") then
			humanoidRootPart.CFrame = CFrame.new(position)
		end
	end
end

local function arrestPlayer(player, hours, minutes, seconds)
	local playerName = player.Name

	if occupiedCells[playerName] then
		warn("Cell is already occupied by another player.")
		return
	end

	hours = tonumber(hours) or 0
	minutes = tonumber(minutes) or 0
	seconds = tonumber(seconds) or 0

	if hours < 0 or minutes < 0 or seconds < 0 then
		warn("Invalid time values.")
		return
	end

	local totalTime = hours * 3600 + minutes * 60 + seconds

	-- Teleport the player to the cell
	teleportPlayer(player, Vector3.new(8.12, 3.202, -54.586))
	
	
	-- Start the timer logic
	while totalTime > 0 do
		wait(1)
		totalTime = totalTime - 1

		-- Update the UI on the client side (if needed)

		-- When the timer reaches zero, kill the player and open the door
		if totalTime <= 0 then
			local humanoid = player.Character and player.Character:FindFirstChild("Humanoid")
			if humanoid then
				humanoid.Health = 0
			end

			-- Open the door
			TweenService:Create(game.Workspace.JailCell.JailBars:FindFirstChild("JailDoor"), TweenInfo.new(3, Enum.EasingStyle.Sine, Enum.EasingDirection.Out), {Position = Vector3.new(10.795, 3.82, -61.01)}):Play()
			warn("Timer has reached zero. Opening the door.")
		end
	end

	-- Release the cell when the timer ends
	occupiedCells[playerName] = nil
end

RemoteEvent.OnServerEvent:Connect(function(player, hours, minutes, seconds)
		local playerName = player.Name

		if occupiedCells[playerName] then
			warn("Cell is already occupied by another player.")
			return
		end

		hours = tonumber(hours) or 0
		minutes = tonumber(minutes) or 0
		seconds = tonumber(seconds) or 0

		if hours < 0 or minutes < 0 or seconds < 0 then
			warn("Invalid time values.")
			return
		end

		local totalTime = hours * 3600 + minutes * 60 + seconds

		-- Teleport the player to the cell
		teleportPlayer(player, Vector3.new(8.12, 3.202, -54.586))


		-- Start the timer logic
		while totalTime > 0 do
			wait(1)
			totalTime = totalTime - 1

			-- Update the UI on the client side (if needed)

			-- When the timer reaches zero, kill the player and open the door
			if totalTime <= 0 then
				local humanoid = player.Character and player.Character:FindFirstChild("Humanoid")
				if humanoid then
					humanoid.Health = 0
				end

				-- Open the door
				TweenService:Create(game.Workspace.JailCell.JailBars:FindFirstChild("JailDoor"), TweenInfo.new(3, Enum.EasingStyle.Sine, Enum.EasingDirection.Out), {Position = Vector3.new(10.795, 3.82, -61.01)}):Play()
				warn("Timer has reached zero. Opening the door.")
		end
	end
end)

playerId is not a valid member of Player “Players.TobiElReyofficial758”
I have the problem in the serverscript at line 31
I need some help
It also only jails the player who presses the button and not the selected player.
plus the timer does not work.

2 Likes

Sorry I’ve not had chance to read the whole script closely, but, the issue with the error is probably because you need to get a player.UserId (instead of player.playerID)

I know but it should be defined already as it says playerid = player.UserId

This function is called via remote event to the server, so the first parameter is the player, the second is the passed data.
Try changing this to:

local function updateTimerDisplay(playerWhoActivatedEvent, data)
    local playerId = data.playerId
    --rest of your code

Argument 1 missing or nil(Character Limit)

Which line and script is it from?
This sort of error could be caused by almost anything.

local player = game.Players:GetPlayerByUserId(playerId)

try printing the data value.
Either the data table is not creating the values (but I can’t see the problem if it is),
Or, the data is simply not being passed.

I sent you both of the scripts. You can see where it is.

It does not print anything. It just says Argument 1 missing or nil(Character Limit)