Update a Value every second a player is inside a specific part

Alright so, i’ve been trying to create the following in my game;

Every second that a player is inside of a specific part, a value inside their player thing must be +1.
So basically, if I touch a specific part. A value inside of my player. (see image) would get +1 every second that i’ m inside it.

image

The value nessesary in this case is “radExposed”, I have no idea how to do this. In the past this could’ve been done with findPartsinVector3withWhitelist or something, but that’s deprecated now. So… help please! Thanks :smiley:

You should use ZonePlus. You can use the .playerEntered() event to check when a player enters a specifig zone.
Something like this:

local ZoneModule = require(path.to.Zone)
local RadioactiveZone = ZoneModule.new(path.to.part)
local inZone = {}

RadioactiveZone.playerEntered:Connect(function(player)
   inZone[player] = true

   while inZone[player] do
      --do stuff
   end
end)

RadioactiveZone.playerExited:Connect(function(player)
   inZone[player] = false
end)
3 Likes

I agree that ZonePlus is a convenient way to solve this. However, another approach that won’t require installing an entire module is:

local Players = game.Players

local parts = {}

local detectOn = true
local interval = 1

local op = OverlapParams.new()

local function handleParts(partsHit)
    for _, hitPart in ipairs(partsHit) do
        local playerHit  = Players:GetPlayerFromCharacter(hitPart.Parent)

        if not playerHit then continue end

        playerHit.Values.radExposed.Value += 1
    end
end

task.spawn(function()
    while detectOn do
        for _, v in ipairs(parts) do
            handleParts(workspace:GetPartsInPart(v, op))
        end

        task.wait(interval)
    end
end)

This is of course not a finalized version, but should give you an idea of how to proceed if you decide against ZonePlus.

Also note, if your part has a simple geometry (specifically a sphere or rectangular prism, you can alternatively use GetPartBoundsInBox or GetPartBoundsInRadius for improved performance).