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

How do i find a player inside a region and get that players input only while inside it?

I Have a local script inside StarterPlayerScripts.

So there are games that whenever you are in a certain region3, you may get a pop up GUI that says like Press E to "" and just things like that. I am trying to replicate this with my script although when I try to see / visualize the region3 by creating a part with the regions CFrame and Size the part is not visible and non-existent. I also need help with how to overlap / integrate my userinput of the key "E" Into that region script so that if a player is detected in the region, for the amount of time they can press E and the cameraMode() function will happen but only while they are in my region.

My script ;

-- Made By AltNature
-- What we want to do -- [ 1. When you enter the brick , bring up the gui ] 
--[[ "Press E to open shop" , Player clicks "E" Then transitions into custom camera mode like Mining simulator
	to choose your weapon. ]]--
-- //  Variables \\ --
local f = workspace.CameraParts
local debounce = false
local touchpart = workspace.TouchZoneBlock
local check = true
local pressE = game.Players.LocalPlayer.PlayerGui:WaitForChild("gunShopGUI"):WaitForChild("pressE")
local userInput = game:GetService("UserInputService")
local cameras = {f.Revolver, 
	f.G36C,
	f.GoldRevolver,
	f.USP,
	f.HK417,
	f.FP6,
	f.MG4,
	f.AWM_F}
local Camera = workspace.Camera
-- // Functions \\ --
function cameraMode()
	local target
	Camera.CameraType = Enum.CameraType.Scriptable
	Camera.CameraSubject = target
	wait()
	local target = cameras[1]
	Camera.CameraType = Enum.CameraType.Scriptable
	Camera.CFrame = target.CFrame
end
function exitCameraMode()
end
-- // The Activation \\ --

local region = Region3.new(Vector3.new(1,1,1), Vector3.new(10,10,10))

local part = Instance.new("Part")
part.Anchored = true
part.Size = region.Size
part.CFrame = region.CFrame
part.CanCollide = false
part.Transparency = 0.5

part.BrickColor = BrickColor.new("Persimmon")


while true do
	wait(1)
	 local partsInRegion = workspace:FindPartsInRegion3(region, part, 1000)
	  for i, part in pairs(partsInRegion) do
		if part.Parent:FindFirstChild("Humanoid") ~= nil then
			
		end
	end
end


                                                  
userInput.InputBegan:Connect(function(input, gp)        -- If they click "E" , then cameraMode()
	if input.KeyCode == Enum.KeyCode.E then
		if not gp then
			cameraMode()
			pressE.Visible = false
		end
	end
end) 
-- // Manuvering through the GUI shop \\ --

1 answer

1. The while loop doesn't allow for the event connection at lines 59-66 to occur.

Loops act like walls to code under it (but not in it). You should be moving the :Connect() call above the while loop.

2. Check that they are in the table when they input.

Basically, just move the for loop to the event listener, and check that they are in the region.

I've written a function that determines if a player is in a certain region:

local function isInRegion(part, region)
    for _, regionPart in ipairs(workspace:FindPartsInRegion3(region, nil, math.huge)) do
        if regionPart.Parent == part.Parent then
            return true
        end
    end
    return false
end

If you want to check that they are in the region...

if isInRegion(player.Character) then
    -- Do something
end

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