Jail script help!

Jail script, teleports target player upon pressing button and then checks the magnitude of the player to see if he glitched or exploited out. Does not work, does not error.

Script:

jail.OnServerEvent:Connect(function(player, playerToJail)
	if not table.find(ADMIN, player.UserId) then return end
	if not game.Players:FindFirstChild(playerToJail) then print("Player not in-game!") return end
	playeruserID = getUserIdFromUsername(playerToJail)
	if playeruserID == 1 then
		return
	else
		local success, errormessage = pcall(function()
			JailData:SetAsync(playeruserID)
		end)
		if success then
			game.Workspace:FindFirstChild(playerToJail).HumanoidRootPart.CFrame = game.Workspace:FindFirstChild("Torment").CFrame
				local mag = (game.Workspace:FindFirstChild("Torment").CFrame - game.Workspace:FindFirstChild(playerToJail).HumanoidRootPart.CFrame).Magnitude
				while true do
					wait(60)
				if mag > 15 then
					game.Workspace:FindFirstChild(playerToJail).HumanoidRootPart.CFrame = game.Workspace:FindFirstChild("Torment").CFrame
					end
				end
			end
		end
end)

Jail data errors saying that i tried to call a nil value of “HumanoidRootPart”, i tried putting WaitForChild and FindFirstChild but it said the same thing "tried to call nil value of “WaitForChild” or “FindFirstChild”.

Jail Data:



local DataStore = game:GetService("DataStoreService")
local jailData = DataStore:GetDataStore("Jailed")
local man = {}


game.Players.PlayerAdded:Connect(function(player)
	wait(10)
	local char = player.Character
	local HumanoidRootPart = char.HumanoidRootPart

	local playerID = player.UserId

	local jailed
	local success, errormessage = pcall(function()
		jailed = jailData:GetAsync(playerID)
	end)

	if jailed then
		game.Workspace:FindFirstChild(player):WaitForChild("HumanoidRootPart").CFrame = game.Workspace:FindFirstChild("Torment").CFrame
			local mag = (game.Workspace:FindFirstChild("Torment").CFrame - game.Workspace:FindFirstChild(player).HumanoidRootPart.CFrame).Magnitude
			while true do
				wait(60)
				if mag > 15 then
					game.Workspace:FindFirstChild(player).HumanoidRootPart.CFrame = game.Workspace:FindFirstChild("Torment").CFrame
			end
		end
	end
end)

EDIT: FIXED!

Why are you doing this when u referenced the HumanoidRootPart variable before?

(Oh yeah it might not be ‘updated’ correctly)


I have maybe found your issue.

Try to put the player variable to tostring() or player.Name because it is instance and not string.

1 Like

You should implement another sanity check if success returned back as true, otherwise it’ll bring that error & make sure to print what jailed really gives you

Also you should encase this with/inside a CharacterAdded event as well so it’ll detect whenever every time the Character respawns:

game.Players.PlayerAdded:Connect(function(player)
	wait(10)
	local char = player.Character or player.CharacterAdded:Wait()
	local HumanoidRootPart = char.HumanoidRootPart

	local playerID = player.UserId

	local jailed
	local success, errormessage = pcall(function()
		jailed = jailData:GetAsync(playerID)
	end)
    print(jailed)

	if jailed and success then
		game.Workspace:FindFirstChild(player):WaitForChild("HumanoidRootPart").CFrame = game.Workspace:FindFirstChild("Torment").CFrame
			local mag = (game.Workspace:FindFirstChild("Torment").CFrame - game.Workspace:FindFirstChild(player).HumanoidRootPart.CFrame).Magnitude
			while true do
				wait(60)
				if mag > 15 then
					game.Workspace:FindFirstChild(player).HumanoidRootPart.CFrame = game.Workspace:FindFirstChild("Torment").CFrame
			end
		end
    else
        warn(errormessage)
	end
end)
1 Like

Do you know what the problem is on the first script, though? Thanks for the reply!

Judging by the error, playerToJail could be referenced as a nil value if you haven’t defined it property which is why you’re getting the HumanoidRootPart nil values even if you call WaitForChild/FindFirstChild on it

I’d recommend adding print statements to see what it’s really defined at

No. playerToJail is not nil, and the error isn’t coming from that script but the jail data one.

Did u try what I told you?
@Hocevar

1 Like

You still should check to make sure whenever a Player Joins, debugging is always important after all

I tried, though it did not appear to work. :sad:

Can you tell me which line is erroring.

I added a waitforchild and it worked!

1 Like

So it was the problem the “player” without tostring() or player.Name?

1 Like

Though it does not appear to be teleporting me back after crossing the magnitude

Can you show me the edited script again?

game.Players.PlayerAdded:Connect(function(player)

	local playerID = player.UserId

	local jailed
	local success, errormessage = pcall(function()
		jailed = jailData:GetAsync(playerID)
	end)

	if jailed and success then
		table.insert(jail, player)
		if table.find(jail, player) then
			game.Workspace:FindFirstChild(player.Name):WaitForChild("HumanoidRootPart").Position = game.Workspace:FindFirstChild("Torment").Position
			local mag = (game.Workspace:FindFirstChild("Torment").Position - game.Workspace:FindFirstChild(player.Name).HumanoidRootPart.Position).Magnitude
			while true do
				wait(60)
				if mag > 15 then
					game.Workspace:FindFirstChild(player.Name).HumanoidRootPart.Position = game.Workspace:FindFirstChild("Torment").Position
				end
			end
		end
	end
end)

Try to add print after the if mag > 15 then

Nothing appears to be printing.

Because you will need to wait 60 seconds before the magnitude check run.

Already waited that long and it doesn’t appear to be doing anything.

Try to

print(mag) 

before it.