Shop Gui constantly enabling and disabling when in region3

Hello all,
First i’d like to point out that this is my first post, so correct me if i’m doing anything wrong.
I am trying to make a Gui (complete) that will enable and stay enabled as long as the player is in the region3.

The shop does enable when I enter the region3, however it constantly flashes on and off when I enter the region.

I have tried using the wait() function to make this successful, but it just flicks on and off depending on the length of the wait() time. Keep in mind I am still a beginner at scripting and I don’t have much knowledge of advanced functions/concepts, so I am making this post.

TL;DR I am making a Gui that should turn on and stay on when a player enters a region3 and turn off immediately when the player exits.

Here is my code (Placed in a LocalScript):

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 wait(1) do
	local partsInRegion = workspace:FindPartsInRegion3(region, nil, 1000)
	for i, v in pairs(partsInRegion) do
		if v.Parent:FindFirstChild("Humanoid") ~= nil then
			print("Player"..v.Parent.Name.."is in region")
			script.Parent.Parent.Shop.Enabled = not script.Parent.Parent.Shop.Enabled
		end
	end
end


Thanks!
-Sctyrant

Instead of checking if its a player, check if its the local player so that way it doesn’t enable/disable when another player is in it.

Make a variable if a player is in the region and make it true or false corresponding to if they’re in or not.

At the end of the loop, check if the variable is true or false and make it enabled or disabled, enabled if they’re in and disabled if they’re out.

I hope this helps!

Thank you, this helps a lot, however I am not really sure how to check for local players instead of players, would you mind sending one or two lines of code that would show me how to do it?

As it’s a local script, we should be able to change the check from this

if v.Parent:FindFirstChild("Humanoid") ~= nil then

to this

if game.Players:GetPlayerFromCharacter(v.Parent) == game.Players.LocalPlayer then

Thanks so much! The script is now fully client sided and no one else would get this changed.
I have added a return statement at the end of the variable that checks if the player is in the region or not.
This has caused another problem, however, and that is that the variable now will no longer change to false.

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
			local playerIsInRegion = true
			if playerIsInRegion then
				script.Parent.Parent.Shop.Enabled = not script.Parent.Parent.Shop.Enabled
			end
			return playerIsInRegion
		end
	end
	
	
end

Here is the current script.

You got it slightly wrong. Making the variable there won’t change anything. Use this:

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

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
            if not playerIsInRegion then
                playerIsInRegion = true
				script.Parent.Parent.Shop.Enabled = not script.Parent.Parent.Shop.Enabled
			end
		end
	end
    playerIsInRegion = false
	
	
end

This change makes it so that it enables it once instead of multiple times since theres multiple parts inside the character.
Sorry for the late response.

1 Like

Or alternatively, you could just break the loop.

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

Yes, thank you. It works smoothly now!

@Sctyrant

I tried to do this with your code provided, however it no longer works for me. I’m not sure if it’s the code or me, but what could i be doing wrong?

Hey there, I actually tweaked @skillednames’s code without explicitly stating it.

I set the enabling/disabling of the GUI after the for loop as follows:

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)
    local isInRegion = false
    for i, v in pairs(partsInRegion) do
        if game.Players:GetPlayerFromCharacter(v.Parent) == game.Players.LocalPlayer then
            isInRegion = true
        end
    end
    
	if isInRegion == true then
		script.Parent.Parent.Shop.Enabled = true
	else
		script.Parent.Parent.Shop.Enabled = false
	end
end

This should work now.

2 Likes