How do I check if a player is within an areea of an object?

Stupid question, but how can I make it so when a player enters the areea marked with red, a GUI pops up, and when the player leaves that areea, the GUI disappears ?

help

Right now the script I’m making is in a LocalScript inside StarterPlayerScripts.

What’s the easiest way to do this ?
Please help.

1 Like

You could do this by using Part.Touched and TouchEnded. I linked the api page, good luck.

2 Likes

You can try using Region3. You can loop through each player, and see if there character is in the area. If they are, make the GUI visible, otherwise make it invisible. I don’t know if this is the most efficient but you can experiment around! :smiley:

Here is a script you can use:

local Part = PathToPart
local Gui = PathToGui

Part.Touched:Connect(function(hit)
	if hit.Parent == game.Players.LocalPlayer then
		Gui.Enabled = true
	end
end)

Part.TouchEnded:Connect(function(hit)
	if hit.Parent == game.Players.LocalPlayer then
		Gui.Enabled = false
	end
end)
2 Likes

This will change the area to a circle, but you could check the distance between the player and the object.
I do not know if your green house thing is a model, union, or mesh, so if it’s a model you’ll probably want to get the position of its PrimaryPart, if it’s a union or mesh then you obviously just get the part’s position.
Here’s an example of what you might want (In this example I’ll code as if it were a model):

local item = workspace.GreenHouse.PrimaryPart -- set a variable
local player = game.Players.LocalPlayer
local character = player.Character
local primary = character.PrimaryPart

-- PLEASE READ THIS SECTION --
-- I don't know where your gui is, so I'm going to assume its in the PlayerGui, and I'll toggle its enabled property. Please edit if needed.
local playerGui = player.PlayerGui
local GUI = playerGui.Popup
GUI.Enabled = false

local dist = 20 -- the distance between the player and the item in... I don't know what type of units?
-- I guess you should just try playing around with numbers

local findhighestvalue = function(val1, val2)
    if math.abs(val1) > math.abs(val2) then -- get the absolute value
         return 1
    else
        return 2
    end
end

local getHigherDist = function(dim)
    if dim == "x" then
        local higherx = findhighestvalue(primary.Positon.X, item.Position.X)
    elseif dim == "y" then
        local higherx = findhighestvalue(primary.Positon.Y, item.Position.Y)
    else
        -- dim == z
        local higherx = findhighestvalue(primary.Positon.Z, item.Position.Z)
    end
    local lowerx = nil
    local totalx = nil
    if higherx == 1 then
        -- character's x is higher
        higherx = primary.Position.X
        lowerx = item.Position.X
        totalx = math.abs(higherx) - (lowerx)
    else
        -- item's x is higher
        higherx = item.Position.X
        lowerx = primary.Position.X
        totalx = math.abs(higherx) - math.abs(lowerx)
    end
    return totalx
end

local findDistance = function()
    if (getHigherDist("x") <= dist) and (getHigherDist("y") <= dist) and (getHigherDist("z") <= dist) then
        return true
    else
        return false
    end
end

while wait() do -- this part runs the functions
    local closer = findDistance()
    if closer then
        GUI.Enabled = true -- MORE GUI ENABLE STUFF --
    else
        GUI.Enabled = false
    end
end

anyway, this is a bit advanced, if you want something simple you can try @Hadiisepic’s idea below this post.

1 Like

You could try placing a invisible part and cancolide it off which detects if the user has touched the part.