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

How to check if the player is in a certain area?

Asked by
d1_rek 46
4 years ago

So I'm trying to figure out how to check if a player is in a specific area in the map, but I don't know what method to use. Basically I want it so while the player is in an area a value is set in their player to true. This would then communicate with other scripts to make an effect.

1 answer

Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

This is pretty simple. You can use Region3.new() . That is one method. You can put a Part in the area that you want the player to be in. Region3

This is the script

local part = -- Put the part path here.
local value = -- Put the value name here(In a string, else it will not work), Has to be in the player and not in one of the children of the player
function CreateRegion3(Part)
    return Region3.new(Part.Position-(Part.Size/2), Part.Position+(Part.Size/2))
end

function FindPlayerInRegion3FromPart(PartToFindPlayerIn)
    local partForRegion = CreateRegion3(PartToFindPlayerIn)
    local playersInRegion3 = workspace:FindPartsInRegion3(partForRegion, nil, math.huge)

    for i, v in pairs(playersInRegion3) do
        local player = game.Players:GetPlayerFromCharacter(v.Parent)
        if player ~= nil then
            if player:FindFirstChild(value) then
                player[value].Value = true
            end
        end
        return player
    end
end

while wait(0.5) do
    FindPlayerInRegion3FromPart(PartToFindPlayerIn)
end

If this does not work, please comment and I will try to fix it. After this you can do the communicating with scripts for the effect.

0
Thank you so much. But PartToFindPlayerIn is undefined. d1_rek 46 — 4y
0
Oh, my bad. PartToFindPlayerIn at the bottom of the code should be replaced with the part variable defined at the top. messi55313 -2 — 3y
Ad

Answer this question