Teleport a player in a place through datastore and remotes does not work?

Hi, devs! I’m trying to make a jail system. The problem, when I press the “jail” button… Well, errors happen. I’m not sure if I should go into a full nuclear printing or ask you guys if I made a fatal mistake. You press the button and the target player should be teleported into a box, and if his magnitude from the part he’s teleported to is higher than 15 then it teleports the user back. I believe the jail datastore is broken as well? I tried a few methods of doing this before and none of them worked.

QNA

Q: Is this in a localscript?
A: No, it’s a server script.

Q:PlayerToWarn is nil?
A: No, it is not.

Q:Any errors in output?
A: 08:29:26.725 ServerScriptService.ModGUI.Main:214: attempt to index nil with 'Connect' - Server - Main:214 — Erroring at jail.OnServerEvent:Connect

Code:

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.Position = game.Workspace:FindFirstChild("Torment").Position
			table.insert(jail, playerToJail)
			if table.find(jail, playerToJail) then
				local mag = (game.Workspace:FindFirstChild("Torment").Position - game.Workspace:FindFirstChild(playerToJail).HumanoidRootPart.Position).Magnitude
				while true do
					wait(60)
				if mag > 15 then
					game.Workspace:FindFirstChild(playerToJail).HumanoidRootPart.Position = game.Workspace:FindFirstChild("Torment").Position
					end
				end
			end
		end
	end
end)

Data:



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

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

	local playerID = player.UserId

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

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

I’m unsure of what to do.

What does the jail variable contain? If you’re saying it’s erroring on

jail.OnServerEvent:Connect(function(player, playerToJail)

Then maybe something is up with what the jail variable contains? It shouldn’t do that if it references a remote

User is supposed to type in a textbox, the name they write in the textbox should be playerToJail.

maincmd.Jail.MouseButton1Click:Connect(function()
	rp.Jail:FireServer(Target.Text)
end)

localscript^

I’m talking about the jail variable that you’re trying to use OnServerEvent on, if you’re saying it errors on that line, then something is wrong with how you set up that variable because it would’ve set another error otherwise

local jail = AD:FindFirstChild("Jail")

jail is defined as the remote.
AD is the folder that the remotes are stored in.

Could it be that your reference to that variable is being changed somewhere in the code into a table? I think I may need to see the full code to see where a problem may lie

1 Like

Yes… My idiot made a table with the same name…

1 Like