Not finding duplicates in array?

Hey, I’m Darthvader. I’m working on a script that finds a player in a part, and then checks to see what team they’re on. I also had the script open a UI when they entered this region. That’s all well and working, until now.

I wanted to add a way to see if the player had LEFT the block, and so I created two lists to check if the player was in the part, and then check if the player was still in the part afterwards.

  • Players dictates if the player was in the part in the past
  • Temp checks if the player is in the part currently. It then checks if player is both in temp and in players.

The screenshots show not only does it add the player multiple times to both temp and players, it also executes the conditions of the player not being in the part even if he is in the part. The screenshots were both taken on a single run, and they were taken on a breakpoint just after checking “if not Players[v]” even though it clearly was there.

Not sure why it isn’t recognizing the player being in both lists, and im also not sure why it’s adding the player several times.

The remoteevent1 fires to show the GUI on the player’s screen and the remoteveent2 fires to remove that gui from the players screen.
The capturepoint functions and associated items work fine and as normal, however both the remoteevents continue to fire repeatedly even though it should be remoteevent1 exclusively.

This results in the GUI never appearing.
If you have read this far, I appreciate you taking the time to help out and consider. Thank you for viewing.

Players = {}
while wait(.5) do
	temp = {}
	for _,Part in pairs(game.Workspace:GetPartBoundsInBox(cf,size) ) do
  		if Part.Name == "HumanoidRootPart" or Part.Name == "LeftFoot" or Part.Name == "RightFoot" or Part.Name == "RightLowerLeg" or Part.Name == "LeftLowerLeg" then
			if Part.Parent:FindFirstChild("Humanoid") then
				if Part.Parent.Humanoid.Health > 0 then
					if game:GetService("Players"):GetPlayerFromCharacter(Part.Parent) then
						local plr = game:GetService("Players"):GetPlayerFromCharacter(Part.Parent)
						if not temp[plr] then
							table.insert(temp,plr)
						end
						if not Players[plr] then
							table.insert(Players,plr)
						end
						game:GetService("ReplicatedStorage").Points.Refinery.RemoteEvent1:FireClient(plr)
						if CaptureTeam.Value == plr.Team then
							if CaptureProg.Value ~= tostring(need.Value) then
								CaptureProg.Value = tostring(tonumber(CaptureProg.Value) + 1)
							else
								OwningTeam.Value = plr.Team
							end
						else
							if tonumber(CaptureProg.Value) > 0 then
								CaptureProg.Value = tostring(tonumber(CaptureProg.Value) - 1)
							end
							if tonumber(CaptureProg.Value) <= 0 then
								CaptureTeam.Value = plr.Team
							end
						end
					end

				end
			end
		end
	end
	for k,v in pairs(temp) do
		if not Players[v] then
			game:GetService("ReplicatedStorage").Points.Refinery.RemoteEvent2:FireClient(v)
			table.remove(Players, table.find(Players,Players[v]))
		end
	end
end


image

Have you tried to do:

local function function()
--do what you need
part.TouchEnded:Wait()
end

Thsi should wait until TouchEnded fires

1 Like

Sadly I have tried this, and it doesn’t seem to work properly with what I’ve got set up. I’d also be interested to know why this is occurring with the arrays so I do not run into this in the future.

Issue is done. Had to change syntax in the final conditional to check if a model in players was in temp, and not if a model in temp was in players.

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