How to make a script where it disables a script when being in a certain area

So, I’m wondering how do I make a script that disables a script when being in a certain area. Basically, a script disabling another script when being in a certain area.

Thanks.

You can use this to check if a player is in an area and disable the script if so:

local Part = script.Parent -- invisible part which indicates the area
Part.Touched:Connect(function() end) -- Just for a TouchInterest to occur on a CanCollide false part

local Character = nil -- the players character

local Script = workspace.Script -- script to disable

function PlayerIsInArea(Part,Character)
	local touching = Part:GetTouchingParts()
	for i=1,#touching do
		if touching[i] == Character.HumanoidRootPart then
			return true
		end
	end
	return false
end

if PlayerIsInArea(Part, Character) then
	Script.Disabled = true
end

(most of the code is from the topic I sent)

Do I put this script in the area where the script disables the other script?

There is an error for the HumanoidRootPart for some reason (my game is in R6)

Yeah, sorry I had to go somewhere

did you set the character variable to the players character?

local run = game:GetService("RunService")
local players = game:GetService("Players")

local part = script.Parent
local otherScript = workspace.Script --Some path to the script.

run.Stepped:Connect(function()
	local touchingParts = workspace:GetPartsInPart(part)
	for _, touchingPart in ipairs(touchingParts) do
		local touchingModel = touchingPart:FindFirstAncestorOfClass("Model")
		if touchingModel then
			local touchingPlayer = players:GetPlayerFromCharacter(touchingModel)
			if touchingPlayer then
				otherScript.Disabled = true
			end
		end
	end
end)

This should be relatively simple to follow.

1 Like

I’m making an obstacle course, so how do I make the script disabled while the player is in the part.

And make it enabled when outside of the part.