How can I change this to detect if the players are touching a certain part rather than sitting?

Here is a line of code from a story game I’m making. image It checks if the player is sitting and kills those who did not sit. How can I do something like this if I wanted the players to be standing inside a certain part? Like an invisible safe zone that I can just fill an entire room with 1 Part and match the name of that part in the script and only players who are touching this part will not die when the timer runs out. I just don’t want to place a bunch of seats inside a room, in most story games players are required to stand inside a building or some kind of safe zone before the time runs out so that’s what I’m trying to replicate. Take a look at that picture and try and modify it to work the way I want, thanks!

local Players = game:GetService("Players")

local partToTouch = -- the part in which the players should be


local function doNothing()
end

local function killPlayersWhoAreNotTouching(partToTouch)
	-- Creating a touched connection creates a touchtransmitter that is needed for :GetTouchingParts() to work with CanCollide false parts.
	-- The connected function doesn't need to do anything.
	local touchConn = partToTouch.Touched:Connect(doNothing)
	
	for i, v in ipairs(Players:GetPlayers()) do
		local char = plr.Character
		if not char then
			continue
		end
		local hum, hrp = char:FindFirstChild("Humanoid"), char:FindFirstChild("HumanoidRootPart")
		if not (hum and hrp) then
			continue
		end
		if not table.find(hrp:GetTouchingParts(), partToTouch) then
			hum.Health = 0
		end
	end
	touchConn:Disconnect()
end

local function challenge_l()
	local teacher_Image = "rbxassetid://859175826"
	CreateDialogueEvent:FireAllClients(teacher_Image, "Hurry up and get inside before we lock the door")
	TimerEvent:FireAllClients(10)
	wait(10)
	killPlayersWhoAreNotTouching(partToTouch)
end

I still don’t understand, I will give you an uncopylocked place where you will be able to work on the script and test it to make sure it works. The script is gamescript in ServerScriptService, I shortened it down so it’ll be easier to navigate through and test a lot faster.

script.Parent.Touched:Connect(function(hit)
	local Char = hit.Parent
	local Humanoid = Char:FindFirstChild("Humanoid")
	
	if Humanoid ~= nil and game.Players:GetPlayerFromCharacter(Char) then
		Humanoid.MaxHealth = math.huge
		Humanoid.Health = math.huge
	end
end)

script.Parent.TouchEnded:Connect(function(left)
	local Char = left.parent
	local Humanoid = Char:FindFirstChild("Humanoid")

	if Humanoid ~= nil and game.Players.GetPlayerFromCharacter(Char) then
		Humanoid.MaxHealth = 100
		Humanoid.Health = 100
	end
end)

I don’t know if this is what you mean, but put this script into the safezone part and it should work. :slight_smile:

Nope, that didn’t work at all.

Oh! I just went through your script. It won’t work because the character isn’t sitting lol. I’ll edit your script to make it work.

Yes that’s what I want to be changed, try and see if you can replace the sitting with standing in that green safe zone.

I tested in your place and found some small mistakes in my code. After fixing them it seems to work. Here’s the fixed code.

local Players = game:GetService("Players")

local partToTouch = workspace.SafeZone


local function doNothing()
end

local function killPlayersWhoAreNotTouching(partToTouch)
	-- Creating a touched connection creates a touchtransmitter that is needed for :GetTouchingParts() to work with CanCollide false parts.
	-- The connected function doesn't need to do anything.
	local touchConn = partToTouch.Touched:Connect(doNothing)

	for _, plr in ipairs(Players:GetPlayers()) do
		local char = plr.Character
		if not char then
			continue
		end
		local hum, hrp = char:FindFirstChild("Humanoid"), char:FindFirstChild("HumanoidRootPart")
		if not (hum and hrp) then
			continue
		end
		if not table.find(hrp:GetTouchingParts(), partToTouch) then
			hum.Health = 0
		end
	end
	touchConn:Disconnect()
end

local function challenge_1()
	local teacher_Image = "rbxassetid://859175826"
	CreateDialogueEvent:FireAllClients(teacher_Image, "Hurry up and get inside before we lock the door")
	TimerEvent:FireAllClients(10)
	wait(10)
	killPlayersWhoAreNotTouching(partToTouch)
end

Can you also send me back a copy of the place so I can examine where the fix was made?

We shouldn’t have to sit to be safe.rbxl (100.6 KB)

2 Likes

Everything is working now, thanks for your help! I will let you know if I have any future problems because I plan to use more than one of these safe zones for the game.

1 Like