Trying to make it so that when the player is in a region a gui pops up, when they are outside it disappears. Very simple and I just can’t seem to get it. I’ve tried just about every solution on the dev forum. Still didn’t work.
local player = game.Players.LocalPlayer
local RegionPart = game.Workspace.Regions.ShopRegion
local shopGui = player.PlayerGui:WaitForChild("Shop"):WaitForChild("Main")
while wait(.1) do
print("started")
for i, section in pairs(RegionPart:GetChildren()) do
local pos1, pos2 = (section.Position - (section.Size/2)), (section.Position + (section.Size/2))
local region = Region3.new(pos1, pos2)
local playersFound = game.Workspace:FindPartsInRegion3(region)
for i, playersInArea in pairs(playersFound) do
if playersInArea:FindFirstAncestor(player.Name) then
if shopGui.Visible ~= true then
shopGui.Visible = true
end
print("Shop Gui visible")
else
shopGui.Visible = false
print("Shop Gui invisible")
end
end
end
end
The script it also located in the GUI I want to pop up.
You have a way of checking if the player is in the region here
local playersFound = game.Workspace:FindPartsInRegion3(region)
But, that only finds players that are in the region so we need to have another loop that checks if the player is outside of the region. This is done by looping through the players found and if the player is not found in the players found then that means they are not in the region. First thing we do is place the players found above so the other loop can access it. Then we make a separate loop to detect if the player is outside using similar code to the first loop but a little bit different. We can also remove the else part of the initial loop.
local player = game.Players.LocalPlayer
local RegionPart = game.Workspace.Regions.ShopRegion
local shopGui = player.PlayerGui:WaitForChild("Shop"):WaitForChild("Main")
local playersFound -- Moved playersFound here
spawn(function() -- Spawn function runs loop in different thread so two loops can run at once
while wait(.1) do
if playersFound then
local InRegion = false
for i, playersInArea in pairs(playersFound) do
if playersInArea:FindFirstAncestor(player.Name) then
InRegion = true
end
end
if not InRegion then
shopGui.Visible = false
print("Shop Gui isnt visible")
end
end
end
end)
while wait(.1) do
print("started")
for i, section in pairs(RegionPart:GetChildren()) do
local pos1, pos2 = (section.Position - (section.Size/2)), (section.Position + (section.Size/2))
local region = Region3.new(pos1, pos2)
playersFound = game.Workspace:FindPartsInRegion3(region)
for i, playersInArea in pairs(playersFound) do
if playersInArea:FindFirstAncestor(player.Name) then
if shopGui.Visible ~= true then
shopGui.Visible = true
end
print("Shop Gui visible")
end
end
end
end
local Player = game:GetService("Players").LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local RootPart = Character:WaitForChild("HumanoidRootPart")
local RegionPart = game.Workspace.Regions.ShopRegion
local ShopGui = Player.PlayerGui:WaitForChild("Shop"):WaitForChild("Main")
while true do
local visible = false
for _,v in pairs (RegionPart:GetChildren()) do
local pos1, pos2 = v.Position - (v.Size / 2), v.Position + (v.Size / 2)
local region = Region3.new(pos1, pos2)
for _, k in pairs (workspace:FindPartsInRegion3WithWhiteList(region, {RootPart}, 10) do
if k == RootPart then
visible = true
end
end
end
ShopGui.Visible = visible
wait(0.25)
end
I do not have the script for you, BUT here’s what you’ve did wrong.
You tried to do the checkings on the client.
My idea is, just get a server script that handles whenever someone enters the region, checks if they are a player and fire a remote event, get a local script to listen for it and prompt the GUI.
Checking if objects are or are not in a Region3 is an expensive task. If you put that load on the server, especially in a constant loop that is running, there is the potential for performance issues down the line. There is no need to put that task on the server when each client is more than capable of finding their own position relative to a Region3.
And that’s the use of RunService. It’s just a better alternative for busy loops.
Another issue that why it doesn’t work is because either the pos1 and pos2 positions are placed wrongly, or the region position is negative, or the formula to find the corners of the region is wrong. What I suggest is that he can just create 2 separate parts to create the region.
The formulas used to find pos1 and pos2 holds no matter how the Part is placed or sized. Having two parts to define the corners of a region3 is fairly unneccesary and only makes it more inconvenient down the line if the size of the region needs to be adjusted. Again, I really don’t think the server needs to take the time to determine if each player is or isn’t in the region just to control whether or not their shop gui is visible.