Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Can anyone explain why this Jailing System is putting out this error?

Alright so I've been working on a jailing system today, I found some coding on the devforum to help me out as a guideline, was able to fix some of the errors on my own but now I'm a bit stuck.

What I want in the end for this script to do is to send a player to a cell (1 of 6 cells)l, and keep them in there for a set amount of time, which is 5 minutes in my case; and of course it'll save even if they leave.

This is the main code:

local dss = game:GetService('DataStoreService')
local Players = game:GetService('Players')
local DS = dss:GetDataStore('JAILDS')
local jailedPlayerdata = {}
local rm = game:GetService('ReplicatedStorage')
local remote = rm.jailE
local cells = game.Workspace.cells

local function jailPlayer(player)
	print(player.Name)
	-- this is where they'll get sent to jail no working code yet.
	spawn(function()
		while jailedPlayerdata[player.UserId]['jailTime'] >= 0 and player do
			jailedPlayerdata[player.UserId]['jailTime'] = jailedPlayerdata[player.UserId]['jailTime'] - 1
			wait(1)
		end
		if player then
			jailedPlayerdata[player.UserId]['jailTime'] = 0
			player:LoadCharacter()
		end
	end)
end

remote.OnServerEvent:Connect(function(player, tPlayer, timeSet)
	jailedPlayerdata[tPlayer.UserId] = timeSet
	jailPlayer(tPlayer)
	print('Data received: '.. tPlayer.Name.. ', '.. timeSet)
end)

Players.PlayerAdded:Connect(function(player)
	local success, data = pcall(function()
		return DS:GetAsync(player.UserId)
	end)
	
	if success then
		if data then
			jailedPlayerdata[player.UserId] = data
			if data['jailTime'] > 0 then
				jailPlayer(player)
			end
		else
			jailedPlayerdata[player.UserId] = {jailTime = 0}
		end
	else
		warn("Jail data failed to load")
	end
end)

Players.PlayerRemoving:Connect(function(player)
	local success, errorMsg = pcall(function()
		DS:SetAsync(player.UserId, jailedPlayerdata[player.UserId])
	end)
end)

This is the code I use in the cuff tool:

local rm = game:GetService('ReplicatedStorage')
local remote = rm.jailE
local Players = game:GetService('Players')
local timeSet = 300

script.Parent.Equipped:Connect(function(mouse)
	mouse.Button1Down:Connect(function()
		if mouse.Target and mouse.Target.Parent:FindFirstChild('Humanoid') then
			local tPlayer = Players:GetPlayerFromCharacter(mouse.Target.Parent)
			remote:FireServer(tPlayer, timeSet)
			print('Event fired | '.. tPlayer.Name .. ' should be arrested!')
		end
	end)
end)

This is the output I'm getting:

  Player1
  Data received: Player1, 300
  00:25:11.632 - ServerScriptService.data:15: attempt to index field '?' (a number value)
00:25:11.635 - Stack Begin
00:25:11.635 - Script 'ServerScriptService.data', Line 15
00:25:11.635 - Stack End

1 answer

✓ Accepted answer

I think your issue is on line 25 of the first script where you have the line:

jailedPlayerdata[tPlayer.UserId] = timeSet

I'm guessing timeSet is a number, so when you try to index that value in the jailPlayer function like this..

jailedPlayerdata[player.UserId]['jailTime']

..you are actually trying to index a number like the error message says.

You likely want to change line 25 of the first script to read:

jailedPlayerdata[tPlayer.UserId]['jailTime'] = timeSet

This discussion is preserved as it was. New answers, comments, and votes are closed.