Script works only with 1 player at a time

local RunService = game:GetService("RunService")
local Players = game:GetService("Players")
local ZonePart = script.Parent
local Touching = false

local function isInsideBrick(Pos, Part)
	local v3 = Part.CFrame:PointToObjectSpace(Pos)
	return (math.abs(v3.X) <= Part.Size.X / 2)
	and (math.abs(v3.Y) <= Part.Size.Y / 2)
	and (math.abs(v3.Z) <= Part.Size.Z / 2)
end

local function getPlayersInsideZone(Zone)
	List = {}
	for _, Player in pairs(Players:GetPlayers()) do
		if (Player.Character) then
			local RootPart = Player.Character:FindFirstChild("HumanoidRootPart")
			if (RootPart and isInsideBrick(RootPart.Position, Zone)) then
				table.insert(List, Player)
			end
		end
	end
	return List
end

RunService.Heartbeat:Connect(function()
	if (#getPlayersInsideZone(ZonePart) > 0) then
		if not Touching then
			Touching = true
			CurrentPlayer = List[1]
			print(CurrentPlayer.Name .. " has entered")
		end
	else
		if Touching then
			Touching = false
			print(CurrentPlayer.Name .. " has left")
		end
	end
end)

is there any way to make this script work with multiple players?

Your script has a few things wrong with it, first, CurrentPlayer can only be one person, so that answers your question… Second, I would presume this is in ServerScriptService… Might I suggest making it a ModuleScript (so that other scripts could check whats within the part)… Third, Using Heartbeat a lot can get a bit laggy, (for the server), Try part.Touched (If its invisible and cancollide false then you using part.Touched will give it a TouchDetector). Maybe try Region3, Ive never used Region3 but I think thats exactly what you need…

Here i make one, but din’t work any solution?

local PartTest = script.Parent

local pos1 = PartTest.Position - (PartTest.Size / 2)
local pos2 = PartTest.Position + (PartTest.Size / 2)

local region = Region3.new(pos1,pos2)


local Found = false

while wait(1) do
	local partFind = game.Workspace:FindPartsInRegion3WithWhiteList(region, game.Players.LocalPlayer.Character:GetDescendants())
	
	Found = false
	
	for _, part in pairs(partFind) do
		if part:FindFirstAncestor(game.Players.LocalPlayer.Name) then
			print("Player was found")
			Found = true
			break
		else
			Found = false
			print("Player was not found")
		end
	end
	
	
	if Found == true then
		game.Players.LocalPlayer.Character:FindFirstChild("Humanoid"):TakeDamage(1)
	else
		game.Players.LocalPlayer.Character:FindFirstChild("Humanoid").Health = game.Players.LocalPlayer.Character:FindFirstChild("Humanoid").Health + 1
	end
end`

Is that a modulescript? You need a table then return the table. As I said, I dont know anything about Region3. This needs to be a localscript as your use of LocalPlayer

1 Like

no it is a local script, you said you never worked with region 3, so never mind

Based on my (very little) knowlege of Region3, that should work… Is there any errors in the output?

No

(No ReAsOn FOr MaKe a Big tExT)

Try printing partFind…

print(partFind)

after it is defined in the while statement

nothing printed on the console, the local script is in the part

thats it, The part is in the workspace, Localscripts dont run in the workspace, I would put it in StarterPlayerScripts

1 Like