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

HELP PLEASE!!! How can I find if there is a player in a Designated Area?

Hi,

I am making a Boss fight script, where the entrance will only open if there are no players left inside the arena.

I have Made a part that is the detector and covers the entire interior of the arena script.Parent.Insidebattle, I just need to find a way of detecting whether there is or isn't a player in there.

I have tried the following codes, and included the errors below each.

Code1

local Players = {} -- players in region gets stored into this table
local AreaPart = script.Parent.Insidebattle

function createRegion3FromPart(part)
	local Size = part.Size
	local Position = part.Position
	return Region3.new(Position-(Size/2), Position+(Size/2))
end

function getPlayersInPart(part)
	if part and part:IsA("BasePart") then 
		local newRegion = createRegion3FromPart(part)
		local partsInRegion = workspace:FindPartsInRegion3(newRegion, nil, math.huge)
		local tempList = {} --this list resets each time
		for i, v in pairs(partsInRegion) do 
			if game.Players:FindFirstChild(v.Parent.Name) then 
				local player = game.Players[v.Parent.Name]
				table.insert(tempList, player)
				if not Players[player] then
					table.insert(Players, player)
				end
			end
		end
		for k,v in pairs(tempList) do --loop over every player found in this single check
			if not Players[v] then --if player hasn't been found which was found earlier, 
				table.remove(Players, v) --remove the player.
			end
		end
	end
end


while true do
	for i, v in pairs(Players) do
		print(tostring(v))
	end
	getPlayersInPart(AreaPart) wait(1);
end

16:53:27.061 - Workspace.ObbyStructure.L14.L15.Script:26: invalid argument #2 to 'remove' (number expected, got Instance)

Code2

--[[line114]]
			script.Parent.Insidebattle.Touched:Connect(function(hit)
--[[line116]]				if hit.Parent:FindFirstChild("Humanoid") == nil then --check to see if player is still in minigame
					ResetBossFight()
--[[line 118]]					script.Parent.Door.HingeConstraint.AngularVelocity = 1
				else
--[[line 120]]					script.Parent.Door.HingeConstraint.AngularVelocity = -1
					--Continue with BossFight

17:23:47.833 - Workspace.ObbyStructure.L14.L15.Script:116: attempt to index nil with 'FindFirstChild' - when there were no players in.

If Possible, could you advise ways to improve on Code2 as I understand that more.

.

.

.

.

.

Here is my Full script for anybody wondering, and this is the best result I have got so far:

--[[local Players = {} -- players in region gets stored into this table
local AreaPart = script.Parent.Insidebattle

function createRegion3FromPart(part)
	local Size = part.Size
	local Position = part.Position
	return Region3.new(Position-(Size/2), Position+(Size/2))
end

function getPlayersInPart(part)
	if part and part:IsA("BasePart") then 
		local newRegion = createRegion3FromPart(part)
		local partsInRegion = workspace:FindPartsInRegion3(newRegion, nil, math.huge)
		local tempList = {} --this list resets each time
		for i, v in pairs(partsInRegion) do 
			if game.Players:FindFirstChild(v.Parent.Name) then 
				local player = game.Players[v.Parent.Name]
				table.insert(tempList, player)
				if not Players[player] then
					table.insert(Players, player)
				end
			end
		end
		for k,v in pairs(tempList) do --loop over every player found in this single check
			if not Players[v] then --if player hasn't been found which was found earlier, 
				table.remove(Players, v) --remove the player.
			end
		end
	end
end


while true do
	for i, v in pairs(Players) do
		print(tostring(v))
	end
	getPlayersInPart(AreaPart) wait(1);
end

]]






local TweenService = game:GetService("TweenService")
db = false

local function ResetBossFight()
	--L1
	script.Parent.L1R.Position = Vector3.new(-69, 41.25, -19.5)
	script.Parent.L1R.Anchored = true
	script.Parent.L1R.CanCollide = false
	script.Parent.L1R.Transparency = 1
	script.Parent.MoL1.Position = Vector3.new(-69, 39, -34)
	script.Parent.MoL1.Transparency = 1
	script.Parent.MoL1.OOF.Disabled = true
	
	db = false
end



script.Parent.Insidebattle.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid")== nil then --check to see if player is in mingame
		script.Parent.Door.HingeConstraint.AngularVelocity = 1
	else
		script.Parent.Door.HingeConstraint.AngularVelocity = -1
		if db == false then
			db = true
			--start L1
			wait(3)
			local MoL1 = script.Parent:FindFirstChild("MoL1")
			local L1OOF = MoL1:FindFirstChild("OOF")
			local L1R = script.Parent:FindFirstChild("L1R")
			local Rotecount = 0
			
			local info1 = TweenInfo.new(
				10,
				Enum.EasingStyle.Linear,
				Enum.EasingDirection.Out,
				0,
				false,
				0	
			)
			
			local infocomplete1 = TweenInfo.new(
				2,
				Enum.EasingStyle.Circular,
				Enum.EasingDirection.Out,
				0,
				false,
				0	
			)
			
			local goal1 = {Orientation = Vector3.new(0, 360, 0)}
			local goalcomplete1 = {Position = Vector3.new(-69, 41.25, -27.5)}
			local rotateparttween = TweenService:Create(MoL1, info1, goal1)
			local completetween = TweenService:Create(L1R, infocomplete1, goalcomplete1)
			
			--scriptstart
			MoL1.Transparency = 0.35
			L1OOF.Disabled = false
			wait(1.5)
			repeat
				rotateparttween:play()
				rotateparttween.Completed:wait()
				Rotecount = Rotecount + 1
				print(Rotecount)
			until Rotecount == 4
			L1OOF.Disabled = true
			MoL1.Transparency = 1
			wait(1.5) -- add player check here
			--script.Parent.Insidebattle.Touched:Connect(function(hit)
				if not hit.Parent:FindFirstChild("Humanoid")then --check to see if player is still in mingame
					ResetBossFight()
					script.Parent.Door.HingeConstraint.AngularVelocity = 1
				else
					script.Parent.Door.HingeConstraint.AngularVelocity = -1
						local ClickDetector = Instance.new("ClickDetector")
						ClickDetector.Parent = L1R
						ClickDetector.MaxActivationDistance = 32
						local CDt = L1R:WaitForChild("ClickDetector")
					L1R.Transparency = 0.35
					CDt.MouseClick:Connect(function()
						CDt:Destroy()
						L1R.Anchored = false
						completetween:play()
						completetween.Completed:wait()
						L1R.Anchored = true
						L1R.Transparency = 0
						--L1R.CanCollide = true
					end)
					Rotecount = 0
					
					--db = false
				end
			--end)
		end
	end
end)




Any Help or suggestions will be most definitely appreciated.

Thanks,

IncredibleTeamAidan

1 answer

You can't use table.remove(Players, V) as table.remove expects a number value, simply do:

local ToRemove = table.find(Players, v);
if ToRemove then
    table.remove(Players, ToRemove);
end

This works as table.find returns the value of where the Player is in the table.

1 comment

0

How would I use that in the code I have?

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