Character doesn't get removed from a table when they are no longer in a part

I am trying to remove a character from a table when it is no longer inside a certain part

The character does not get removed from the table after no longer colliding with the part

After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

local currentlyInBunker = {}

task.spawn(function()
	while task.wait(0.01) do
		local players = Players:GetPlayers()
		local characters = {}
		for i, player in ipairs(players) do
			if player.Character then
				table.insert(characters, player.Character)
			end
		end

		local overlapParams = OverlapParams.new()
		overlapParams.FilterType = Enum.RaycastFilterType.Include
		overlapParams.FilterDescendantsInstances = characters

		local collisions = workspace:GetPartsInPart(workspace.ExplosionSafeZone, overlapParams)
		
		task.spawn(function()
			for i, obj in ipairs(collisions) do
				if obj.Name == "HumanoidRootPart" then
					local char = obj.Parent
					
					currentlyInBunker[char] = char
				end
			end
		end)
		
		task.spawn(function()
			for i, obj in ipairs(currentlyInBunker) do
				if not table.find(collisions, obj.HumanoidRootPart) then
					currentlyInBunker[obj] = nil
				end
			end
		end)
		
		print(currentlyInBunker)
	end	
end)

Ok, first you want to add a check when you’re adding characters to the currentInBunker to make sure they’re not already in there. Second I would actually use currentlyInBunker as an Array instead of a Map.

		task.spawn(function()
			for i, obj in ipairs(collisions) do
				if obj.Name == "HumanoidRootPart" and not table.find(currentlyInBunker, obj.Parent) then
					local char = obj.Parent
					print("adding char", char)
					table.insert(currentlyInBunker, char)
				end
			end
		end)

		task.spawn(function()
			for i, obj in ipairs(currentlyInBunker) do
				print(obj)
				if not table.find(collisions, obj.HumanoidRootPart) then
					print("removing char", obj)
					table.remove(currentlyInBunker, table.find(currentlyInBunker, obj))
				end
			end
		end)
1 Like

This isnt a good way to check there in a bunker.Additionally,you dont need to check if there in the bunker then remove them.

Try this instead:


local players = Players:GetPlayers()
task.spawn(function()
	while task.wait(0.01) do
        local currentlyInBunker = {}
		local characters = {}
		for i, player in ipairs(players) do
			if player.Character then
				table.insert(characters, player.Character)
			end
		end

		local overlapParams = OverlapParams.new()
		overlapParams.FilterType = Enum.RaycastFilterType.Include
		overlapParams.FilterDescendantsInstances = characters

		local collisions = workspace:GetPartsInPart(workspace.ExplosionSafeZone, overlapParams)
		
			for i, obj in ipairs(collisions) do
				if obj.Name == "HumanoidRootPart" then
					local char = obj.Parent
					
					currentlyInBunker[char] = char
				end
			end
		
	
				
		
		print(currentlyInBunker)
	end	
end)

[/quote]

This is a solution I use to do just that.
Always log new fresh table and only publish result when completed.

local Players = game:GetService("Players")
local currentlyInBunker = {}

task.spawn(function()
	while task.wait(0.01) do
		local Log = {}
		local players = Players:GetPlayers()
		local characters = {}
		for i, player in ipairs(players) do
			if player.Character then
				table.insert(characters, player.Character)
			end
		end

		local overlapParams = OverlapParams.new()
		overlapParams.FilterType = Enum.RaycastFilterType.Include
		overlapParams.FilterDescendantsInstances = characters

		local collisions = workspace:GetPartsInPart(workspace.ExplosionSafeZone, overlapParams)

		for i, obj in ipairs(collisions) do
			if obj.Name == "HumanoidRootPart" then
				local char = obj.Parent
				Log[char] = char
			end
		end
		
		currentlyInBunker = Log

		print(currentlyInBunker)
	end	
end)
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.