How can I see if a player is Inside of zone using heartbeat service?

How can I see if a player is Inside of zone using heartbeat service?

Do you mean RunService.Heartbeat event?

You can’t do this solely with RunService, you have to use something like Region3 to work along.

Yes that is what I mean, forgot the RunService

local Part = Instance.new("Part")
Part.Anchored = true
Part.Transparency = 0.5
Part.Position = Vector3.new(0,6,0)
Part.Size = Vector3.new(12,12,12)
Part.CanCollide = false
Part.Parent = workspace

game:GetService("RunService").Heartbeat:Connect(function()
	local Region = Region3.new(Vector3.new(-Part.Size.X/2,Part.Size.Y/Part.Position.Y,-Part.Size.Z/2),Vector3.new(Part.Size.X/2,Part.Size.Y,Part.Size.Z/2))
	
	local Parts = workspace:FindPartsInRegion3(Region, Part)
	for i,v in pairs(Parts) do
		if v.Parent:FindFirstChild("Humanoid") then
			v.Parent:FindFirstChild("Humanoid"):TakeDamage(0.01)
		end
	end
end)

here is a way you can detect if a player is in a region3 when using Heartbeat, its probably not the best way but it does work if you want to test this put a script in workspace and then paste this

I suggest using ZonePlus which is an excellent resource.

local Zone = require(game:GetService("ReplicatedStorage").Zone)
local container = workspace.AModelOfPartsRepresentingTheZone -- The Part which represents the zone.
local zone = Zone.new(container)

zone.playerEntered:Connect(function(player)
    print(`Player {player.Name} entered the zone`)
end)

zone.playerExited:Connect(function(player)
    print(`Player {player.Name} exited the zone`)
end)
1 Like