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

Way to detect if a player is not in a region3?

Asked by 3 years ago

Hi, I'm trying to detect if there is a player that is not in region3, but I don't understand how!

local Region = Region3.new(Vector3.new(258, -47, -99), Vector3.new(348, -23, 99))

local Workspace = game:GetService("Workspace")

while wait(1) do

    local PartsInRegion = Workspace:FindPartsInRegion3(Region)

    for _,Child in pairs(PartsInRegion) do

        if Child:IsA("BasePart") then

            local BasePart = Child

            if BasePart and BasePart.Parent then

                if BasePart.Parent:FindFirstChild("Humanoid") then

                    local Character = BasePart.Parent

                    local Humanoid = Character.Humanoid

                    local Players = game:GetService("Players")

                    local Player = Players:GetPlayerFromCharacter(Character)

                    if Player then

                        local BoolValue = Character:WaitForChild("IsFighting")

                        BoolValue.Value = true
                    end
                end
            end
        end
    end
end

1 answer

Log in to vote
1
Answered by 3 years ago
Edited 3 years ago

Hello Bankrovers,

My name is YelloMynamesZak and I'd be happy to help you with your problem.

You could try this: Make a part that represents the region. After you're done, name it "Region".

Then, put a localscript in StarterCharacterScripts and type the following:

local Touching = false
local part = workspace.Region
part.Touched:Connect(function()
    Touching = true
    print("Player is  in region")
end)
part.TouchEnded:Connect(function()
    Touching = false
    print("Player is NOT in region")
end)

This’ll work for a local script. If this is a server sided script then you can make a table which has each user’s ID as the index, and the value equal to false. Set it equal to true upon touching the part, then set it to false upon the touch ending. It's much more simpler than the code you provided and can be connected to server scripts via remote events.

Please reply to this answer is you're facing any issues.

Hope this helps!

0
Works! Bankrovers 226 — 3y
0
Make sure to add you code to that script and have fun! Glad it worked! YelloMynamesZak 85 — 3y
Ad

Answer this question