Constantly detect if a player is touching a part

Hey developers!

I was wondering how I would make a script that instead of checking whether the player touched the part once, it checks if the player is still inside the part.

As of now I have a suspicion it has something to do with Region3 but I am not certain.

If you can lead me to any sort of link, video or know yourself please reply!

1 Like

Have you looked into this? BasePart | Roblox Creator Documentation

Edit: Fixed link

1 Like

This is pretty funny I literally just did something like this, ill share the code for you :slight_smile:

local ts = game:FindService("TweenService") or game:GetService("TweenService")
local d1 = workspace.Door1
local d2 = workspace.Door2
local active = false
local istouching = false
local down = false
local cantween1 = true
local cantween2 = false

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(char)
		local invisiblePart = workspace.Button
		local Ignore = char
		spawn(function()
			while wait(.1) do
				local Ray = Ray.new(char.HumanoidRootPart.CFrame.p,Vector3.new(0,-10,0))
				local part = workspace:FindPartOnRay(Ray,Ignore)
				
				if active == false then istouching = false end
				
				if part then
					if part == invisiblePart then
						active = true
					else
						active = false
					end
				end
			end
		end)
	end)
end)

spawn(function()
	game.Workspace.Button.Touched:Connect(function(part)
		if part.Parent:FindFirstChild("Humanoid") then
			istouching = true
			spawn(function()
				while true do
					wait(.1)
					if istouching == true then
						if down == false then
							down = true
							if game.Workspace.Button.Position ~= Vector3.new(-34, 0.825, -10) or cantween1 == false then return end
							local tweenInfo = TweenInfo.new(.125)
							local tween1 = ts:Create(game.Workspace.Button, tweenInfo, {CFrame = (game.Workspace.Button.CFrame * CFrame.new(0,-1,0))})
							tween1:Play()
							spawn(function()
								tween1.Completed:Connect(function()
									cantween1 = false
									cantween2 = true
								end)
							end)
						elseif down == true then
						end
					elseif istouching == false then
						down = false
						if game.Workspace.Button.Position == Vector3.new(-34, 0.825, -10) or cantween2 == false then return end
						local tweenInfo = TweenInfo.new(.125)
						local tween1 = ts:Create(game.Workspace.Button, tweenInfo, {Position = Vector3.new(-34, 0.825, -10)})
						tween1:Play()
						spawn(function()
							tween1.Completed:Connect(function()
								cantween1 = true
								cantween2 = false
							end)
						end)
						break
					end
				end
			end)
		end
	end)
end)

I don’t know if this is the most efficient way to do it, but this way made the most sense in my head. Hopefully this helps you.

You could probably try something like this:

local RunService = game:GetService("RunService");

function PartToRegion3(Part, AddedHeight)
	assert(typeof(Part) == "Instance", "Expected a part to convert into a Region3, got "..typeof(Part))
	
	local PartSize = Part.Size + Vector3.new(0, (AddedHeight or 0), 0)
	local PartCFrame = Part.CFrame

	local max = PartCFrame.p + (0.5 * PartSize);
	local min = PartCFrame.p - (0.5 * PartSize);

	return Region3.new(min, max); --// Returns a new Region3
end

function IsInRegion(Position, Region) --// Determines if a given Vector3 is inside of the given Region3
	assert(typeof(Position) == "Vector3" and typeof(Region) == "Region3", "Incorrect Parameter types")
	
	local RelativePos = (Position - Region.CFrame.p) / Region.Size;

	local inRegion = -0.5 <= RelativePos.X and RelativePos.X <= 0.5
		and -0.5 <= RelativePos.Y and RelativePos.Y <= 0.5
		and -0.5 <= RelativePos.Z and RelativePos.Z <= 0.5

	return inRegion
end

--// Main
local Character = game.Players.LocalPlayer.Character
local MainRegion = PartToRegion3(workspace.YourPart);

RunService:BindToRenderStep("name", Enum.RenderPriority.Character, function()
    if Character then
        local IsInRegion = IsInRegion(Character.HumanoidRootPart.Position, MainRegion);
        
        if IsInRegion then
            print("Player is in region!");
        end
    end
end);