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
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?
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
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.
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
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