Trying to make a gui pop up when you are in your team's base and disappear when you walk out

Hey fellas,

trying to make a buy menu gui that appears when you’re in your team’s base, and disappear when you leave it. If a player on a different team walks in, the gui will not pop up for them. This gui is only visible ever if you’re standing in your teams base.

I don’t know what the best method for this is, but i’m using region3 in this script i have here:

local regionArea = workspace.TestRegion
local positionA = regionArea.Position - (regionArea.Size / 2)
local positionB = regionArea.Position + (regionArea.Size / 2)
local region = Region3.new(positionA,positionB)

while true do wait()
	
	local partsInRegion = workspace:FindPartsInRegion3(region, nil, math.huge)
	for i, v in pairs(partsInRegion) do
		if game.Players:GetPlayerFromCharacter(v.Parent) == game.Players.LocalPlayer then
		   script.Parent.Parent.Shop.Enabled = not script.Parent.Parent.Shop.Enabled
           break
		end
	end
	
	
end

unfortunately, this local script is not working for some reason. When I walk into the “blue” area, the gui flashes on and off very quickly. if the gui is enabled before the test begins, the gui is already on the screen, and begins flashing when i walk into the area.

How can i fix this, and assign a team to this?

5 Likes

Try filtering the parts, use the function :IsDescendantOf() check if it’s a descendant of the player character instance.

You could instead of using region3, use a for loop to find the team the player is on. And have the gui pop up like that. That would be much more simple if you ask me. As for the gui popping up in a certain spot. I would use an invisible block and add a script that utilizes the for loop as stated before. And check if the player is a part of the specified team.

If you need an example here’s a rough draft

local teams = game:GetService('Teams')
local Players = game:GetService('Players')
for _,player in pairs(Players:GetChildren()) do
  if player.Team == teams.YourTeam then
    -- use humanoid.Touched to have the gui pop up then use TouchEnded to have gui disappear
  end
end

Quick Edit: Only gave this example because you said you didn’t know the best way to go about it, this is my take on the best way since I don’t think it’s completely necessary to use region3. Could be wrong but hope it helps.

if collisions on the part are set to false, will it still work?

local teams = game:GetService('Teams')
local Players = game:GetService('Players')
for _,player in pairs(Players:GetChildren()) do
  if player.Team == teams.YourTeam then
   if humanoid.Touched = true
  then game.StarterGui.BuyMenu.Enabled = true
  if humanoid.TouchEnded = true
 then game.StarterGui.BuyMenu.Enabled = false
  end
end

is this what you mean?

Just realized an error I have you. Use part.Touched not humanoid.Touched. Sorry I didn’t include this earlier my bad. Put the local script inside the part and write this

local part = script.Parent
local teams = game:GetService(‘Teams’)
local Players = game:GetService(‘Players’)

for _, player in pairs(Players:GetChildren()) do
  part.Touched:Connect(function(hit)
    local humanoid = hit.Parent:FindFirstChildOfClass('Humanoid')
    if humanoid then
     if player.Team = teams.YourTeam then
    player.PlayerGui.BuyMenu.Enabled = true
   end
  end
 end)
end


for _, player in pairs(Players:GetChildren()) do
  part.TouchEnded:Connect(function(hit)
    local humanoid = hit.Parent:FindFirstChildOfClass('Humanoid')
    if humanoid then
     if player.Team = teams.YourTeam then
    player.PlayerGui.BuyMenu.Enabled = false
   end
  end
 end)
end
local part = script.Parent
local teams = game:GetService(‘Teams’)
local Players = game:GetService(‘Players’)

for _, player in pairs(Players:GetChildren()) do
  part.Touched:Connect(function(hit)
    local humanoid = hit.Parent:FindFirstChildOfClass('Humanoid')
    if humanoid then
     if player.Team = teams.YourTeam then
    player.PlayerGui.BuyMenu.Enabled = true
   end
  end
 end)
end

for _, player in pairs(Players:GetChildren()) do
  part.TouchEnded:Connect(function(hit)
    local humanoid = hit.Parent:FindFirstChildOfClass('Humanoid')
    if humanoid then
     if player.Team = teams.YourTeam then
    player.PlayerGui.BuyMenu.Enabled = false
   end
  end
 end)
end

??

Does it not work? What’s the problem? Tell me if the code is useful so I can make appropriate changes.

1 Like
local part = script.Parent
local teams = game:GetService("Teams")
local Players = game:GetService("Players")

for _, player in pairs(Players:GetChildren()) do
  part.Touched:Connect(function(hit)
    local humanoid = hit.Parent:FindFirstChildOfClass('Humanoid')
    if humanoid then
     if player.Team = teams.Blue then
    player.PlayerGui.BuyMenu.Enabled = true
   end
  end
 end)
end


for _, player in pairs(Players:GetChildren()) do
  part.TouchEnded:Connect(function(hit)
    local humanoid = hit.Parent:FindFirstChildOfClass('Humanoid')
    if humanoid then
     if player.Team = teams.YourTeam then
    player.PlayerGui.BuyMenu.Enabled = false
   end
  end
 end)
end

nothing happened. the equal sign in player.team = teams.yourteam is redlined.

the buymenu is a screengui in startergui. why does it mention playergui?
collisions are turned off for the part btw.

My bad again use ==. Also if you enable it with startergui I’m sure it’d show up for everyone, The playergui is local. That’s how it works I think. If a gui is in startergui, it’s automatically added to the playergui.

1 Like
local part = script.Parent
local teams = game:GetService("Teams")
local Players = game:GetService("Players")

for _, player in pairs(Players:GetChildren()) do
  part.Touched:Connect(function(hit)
    local humanoid = hit.Parent:FindFirstChildOfClass('Humanoid')
    if humanoid then
     if player.Team == teams.Blue then
    player.PlayerGui.BuyMenu.Enabled = true
   end
  end
 end)
end


for _, player in pairs(Players:GetChildren()) do
  part.TouchEnded:Connect(function(hit)
    local humanoid = hit.Parent:FindFirstChildOfClass('Humanoid')
    if humanoid then
     if player.Team == teams.Blue then
    player.PlayerGui.BuyMenu.Enabled = false
   end
  end
 end)
end

nothing in the output, made the edits though.

Hm, change the humanoid variable to

local plr = Players:GetPlayerFromCharacter(hit.Parent)
then just change

if humanoid then

to

if plr then

1 Like
local part = script.Parent
local teams = game:GetService("Teams")
local Players = game:GetService("Players")

for _, player in pairs(Players:GetChildren()) do
  part.Touched:Connect(function(hit)
    local plr = Players:GetPlayerFromCharacter(hit.Parent)
    if player then
     if player.Team == teams.Blue then
    player.PlayerGui.BuyMenu.Enabled = true
   end
  end
 end)
end


for _, player in pairs(Players:GetChildren()) do
  part.TouchEnded:Connect(function(hit)
    local plr = Players:GetPlayerFromCharacter(hit.Parent)
    if player then
     if player.Team == teams.Blue then
    player.PlayerGui.BuyMenu.Enabled = false
   end
  end
 end)
end

nothing happened.

For the second for loop, are you using team Blue as the variable? It’s set to your team. Also I said set the variable to plr not player. Try adding a waitforchild

player.PlayerGui:WaitForChild('BuyMenu').Enabled = true --same this for second loop just set to false

1 Like

“plr” was an unkown symbol?

what do you mean am i using team blue as a variable?

1 Like

where does this go in the script?

1 Like

For some reason in the second for loop it showed as if player.Team == teams.YourTeam. Just now it changed to blue

1 Like

inside the for loop. Replace

player.PlayerGui.BuyMenu.Enabled = true to the line I gave

1 Like
local part = script.Parent
local teams = game:GetService("Teams")
local Players = game:GetService("Players")

for _, player in pairs(Players:GetChildren()) do
  part.Touched:Connect(function(hit)
    local plr = Players:GetPlayerFromCharacter(hit.Parent)
    if plr then
     if player.Team == teams.Blue then
   player.PlayerGui:WaitForChild('BuyMenu').Enabled = true --same this for second loop just set to false
   end
  end
 end)
end


for _, player in pairs(Players:GetChildren()) do
  part.TouchEnded:Connect(function(hit)
    local plr = Players:GetPlayerFromCharacter(hit.Parent)
    if plr then
     if player.Team == teams.Blue then
    player.PlayerGui:WaitForChild('BuyMenu').Enabled = false --same this for second loop just set to false
   end
  end
 end)
end

I decided to give Region3 a try (since I haven’t really messed with it in the past), and here’s what I came up with.

  1. I need to declare an area in which the UI will show up → Region3
  2. How do I find if a player is in a Region3? → Workspace:FindPartsInRegion3WithWhitelist
  3. How can I search for this constantly? → RunService Heartbeat Loop

I put together a small frame just to demonstrate the UI, and it was off to the races.

Some questions you may have:

  1. Why is the Region3 declared using parts?

    • I just wanted to for convenience. You can use any Vector3 values.
  2. Where is the script located?

    • The LocalScript is located inside StarterCharacterScripts
  3. Why would you put it inside StarterCharacterScripts?

    • The answer is two-fold. One is because it’s easier to define the Character, and because we don’t need to spend server resources.
  4. Doesn’t putting it inside StarterCharacterScripts make it easier for people to manipulate the Region3?

    • Probably. But for my own purposes, I’m not using it as a shop. One easy workaround is to check when the player purchases an item whether or not they’re close enough to the shop/base.

I encourage you to give it a try before turning to my solution but feel free to take a look at it.

Resources:
https://developer.roblox.com/en-us/api-reference/function/Workspace/FindPartsInRegion3WithWhiteList
https://developer.roblox.com/en-us/api-reference/datatype/Region3
https://developer.roblox.com/en-us/api-reference/event/RunService/Heartbeat

LocalScript Content
local myRegion = Region3.new(workspace.Region3Test.LowerCorner.Position, workspace.Region3Test.UpperCorner.Position)
local player = game:GetService("Players").LocalPlayer
local UI = player.PlayerGui:WaitForChild("ScreenGui")
local isInside = false

game:GetService("RunService").Heartbeat:Connect(function()
	local humanoid = workspace:FindPartsInRegion3WithWhiteList(myRegion, {script.Parent.HumanoidRootPart})
	if #humanoid > 0 and player.Team == game:GetService("Teams")["myTeam"] then
		print("Teammate found!")
		if not (UI.TeamFrame.Visible) then
			UI.TeamFrame.Visible = true
		end
	else
		if (UI.TeamFrame.Visible) then
			UI.TeamFrame.Visible = false
		end
	end
end)
5 Likes