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

How to make a jail script?

Greetings,

Perhaps the question is a tad basic, but I am literally struggling. I have tried all possible sources of information, but nothing helped. My sole question is how do I manage to make it so when a player touches a part (let's name it pad), their tools are removed and if they reset they will be respawned on that pad.

Basically, like a jail - if they die, their character gets loaded on the pad (inside the cell), not at the Spawn. Also, regarding Data Store, how do I make the above (the whole jail script) work (remember that player X was in jail when they left and if they return, they are put back in jail) each time a player re-enters the game?

(The whole jail script should work and act as it does when the player is on the pad, if the player is not on the pad and leave, they just spawn at the Spawn point when re-entering and the whole jail script does nothing to them).

I would really appreciate help as I am very stuck here.

The very basic code of which I am ashamed (I managed the remove tools part... but I have no idea with the rest):

pad = script.Parent -- The Pad, which is going to be touched

function hit(pad) -- If the pad is touched, then the tools are removed
	if hit == nil then return end
	local h = game.Players:GetPlayerFromCharacter(pad.Parent)
	if h ~= nil then
		local c = h.Backpack:GetChildren()
		for i=1,#c do
			c[i]:remove()
		end
	end
end

script.Parent.Touched:connect(hit)

Thank you, Have a wonderful day!

1 answer

✓ Accepted answer
local pad = script.Parent -- The Pad, which is going to be touched
local range = 5 -- The range of the jail script

local jailedPlayers = {} -- A table to hold the current jailed players

game.Players.PlayerAdded:connect(function(newPlayer)
	-- do data store stuff here
	-- if data store says jail...
	-- jailedPlayers[newPlayer.UserId] = true
	-- then do the rest of the script

	newPlayer.CharacterAdded:connect(function(newChar)
		wait()	-- wait so we can teleport the player
		if (jailedPlayers[newPlayer.UserId] ~= nil) then
			newPlayer.Backpack:ClearAllChildren()
			newChar.Torso.CFrame = pad.CFrame + Vector3.new(0, 2.5 + pad.Size.Y, 0) -- Add on the pad height as well
			local humanoid = newChar:WaitForChild("Humanoid")
			humanoid.Died:connect(function() -- Check when the player dies to see if they are still in the jail
				local rootPart = newChar:findFirstChild("HumanoidRootPart") -- use a magnitude check with the rootpart and pad
				if (rootPart ~= nil) then 
					local checkPos = Vector3.new(rootPart.Position.X, pad.Position.Y, rootPart.Position.Z) -- Check the char's postion using x and z
					if ((checkPos - pad.Position).magnitude > range) then -- Check if they've left the jail (approximately)
						jailedPlayers[newPlayer.UserId] = nil	-- remove the jailed player from the table
						-- player is no longer in jail
					end
				end
			end)
		end
	end)
end)

game.Players.PlayerRemoving:connect(function(playerLeaving) 
	local char = playerLeaving.Character
	if (char ~= nil) then
		local rootPart = char:findFirstChild("HumanoidRootPart") -- use a magnitude check with the rootpart and pad
		if (rootPart ~= nil) then
			local checkPos = Vector3.new(rootPart.Position.X, pad.Position.Y, rootPart.Position.Z) -- Check the char's postion using x and z
			if ((checkPos.Position - pad.Position).magnitude > range) then -- Check if they've left the jail (approximately)
				jailedPlayers[playerLeaving.UserId] = nil	-- remove the jailed player from the table (In case they rejoin the server)
				-- save to a data store saying not in jail...?
			else
				-- save to a data store saying in jail...?
			end			
		end
	end
end)

function padHit(hit) -- If the pad is touched, then the tools are removed
    if (hit ~= nil) then 
		local player = game.Players:GetPlayerFromCharacter(hit.Parent)
		if (player ~= nil) then
			jailedPlayers[player.UserId] = true -- Jail the player using string keys in a table
			player.Backpack:ClearAllChildren()	-- clear the backpack
			-- player is now in jail
		end
	end
end

script.Parent.Touched:connect(padHit)

Be aware this script uses a magnitude check to see if the player should still be in the jail and the range value should be edited at the top.

You could alternatively use the Position and Size of the pad to calculate whether the player is in the Jail.

There were also some flaws with your hit function so I've fixed that for you.

I didn't include any data store code because I'm lazy... (There are some great guides on the wiki)

Also, this script will only remove tools in the backpack and not tools which are currently equipped.

1 comment

0

Thank you very much I really appreciate. I would kiss you... but that would be awkard, heh.

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