Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Why is Region3 not detecting players inside it? :(

Asked by 4 years ago
Edited 4 years ago

Okay soooooo I decided to make a poison gas area cloud that would damage people inside it. Here is the script:

debounce = false
local RegionPart = game.Workspace.Gas.GasHitbox
local pos1 = RegionPart.Position - (RegionPart.Size / 2)
local pos2 = RegionPart.Position + (RegionPart.Size / 2)
local region = Region3.new(pos1,pos2)

while true do
    wait()
    local partsInRegion = workspace:FindPartsInRegion3(region, nil,100)
    for i, part in pairs(partsInRegion) do
        if part.Parent:findFirstChild("Humanoid") ~= nil and debounce == false then
            debounce = true
            print("Player found in region!".. part.Parent.Name)
            local char = part.Parent
            char.Humanoid.Health = char.Humanoid.Health -5
            wait (1)
            debounce = false
        end
    end
end

Keep in mind, I put this in server script service and this mostly worked with one player until two of them are in it. Also it kind of lags behind now so the player takes damage even after they leave the area of effect. Help would be much appreciated.

Thanks!

1 answer

Log in to vote
0
Answered by 4 years ago

All your issues come from the fact you're yielding in the for loop. After iterating over the first instance where a humanoid is found, the humanoid will be damaged, yield for 1 second, then continue the for loop. I'm not so sure why you have a debounce either, considering it'll always be false when the debounce is checked. Maybe you didn't want every part of the character to invoke damage?

Anyways here's my implementation of what a poison gas script may look like:

while true do
    local partsInRegion = workspace:FindPartsInRegion3(region, nil, 100)
    local humanoids = {} --This will store all the humanoids inside the region

    for i, part in pairs(partsInRegion) do
        local humanoid = part.Parent:FindFirstChild("Humanoid")
        if humanoid then
            humanoids[humanoid] = true --Store the humanoid as an index of the table. Indexes are unique, so there should be no duplicate humanoids
        end
    end

    for humanoid in pairs(humanoids) do
        humanoid.Health = humanoid.Health - 5 --You may also want to call :TakeDamage(5) on the humanoid instead *if* you want to respect forcefields
    end

    --Yield for 1 second before continuing the next check
    wait(1)
end
Ad

Answer this question