Im trying to make a player detection system in a for loop. To achieve this, I am searching a region for parts and removing any part that isnt a player part from the table. My current draft works up until there are 4 indexes left in the table and rechecks the region, thus adding the removed parts back to the table. I have tried adding task.wait()s above and underneath the table.remove call, but it always resets the table when the 4th index is reached, no matter how long of a time I give it. Is there something wrong with the way I’m finding the parts index? Or are there weird rules to removing values from arrays that I’m not aware of? Any help would be appreciated!
Server Script:
for i = 1, 20 do
task.wait(0.05)
local playerEffects = game.Workspace:FindFirstChild(player.Name.." Effects")
local partsInHibox = workspace:FindPartsInRegion3(Hitbox, game.Workspace.Baseplate, 20) --Hitbox is predefined as Region3.new(pos1, pos2) [pos1 and pos2 are the bottom left and bottom right corner of a part]
for _, v in pairs(partsInHibox) do
if v.Name == "Head" or v.Name == "Left Arm" or v.Name == "Left Leg" or v.Name == "Right Arm" or v.Name == "Right Leg" or v.Name == "Torso" then
local PlayerTarget = Players:GetPlayerFromCharacter(v.Parent)
print(PlayerTarget)
EarthAttack:FireClient(PlayerTarget, "ScreenShakeTrue") --remoteEvent in ReplicatedStorage
elseif v.Name ~= "Head" or v.Name ~= "Left Arm" or v.Name ~= "Left Leg" or v.Name ~= "Right Arm" or v.Name ~= "Right Leg" or v.Name ~= "Torso" then
local function findValue(array, itemName) --this is literally taken from the roblox documentation about changing array values, I half understand what its saying tbh
for currentIndex = 1, #array do
if array[currentIndex] == itemName then
return currentIndex
end
end
end
local valueFound = findValue(partsInHibox, v.Name)
table.remove(partsInHibox, valueFound)
end
print(partsInHibox)
if partsInHibox == {} then
EarthAttack:FireAllClients("ScreenShakeFalse") --same remoteEvent
end
end
end