local Players = game:GetService("Players")
local TweenService = game:GetService("TweenService")
local Lighting = game:GetService("Lighting")
local Player = Players.LocalPlayer
local PlayerGui = Player.PlayerGui
local Blur = Lighting.Blur
local Camera = workspace.Camera
local Gui = PlayerGui.Gui
local Frames = Gui.Frames
local Buttons = Gui.Buttons
local info1 = TweenInfo.new(0.2, Enum.EasingStyle.Linear, Enum.EasingDirection.In)
local function CloseFrames()
for _, frame in Frames:GetChildren() do
if frame.Visible then
TweenService:Create(Camera, info1, {FieldOfView = 70}):Play()
TweenService:Create(Blur, info1, {Size = 0}):Play()
frame.Visible = false
end
end
end
local function OpenFrame(frame: Frame)
if not frame.Visible then
CloseFrames()
frame.Visible = true
frame.Position = UDim2.fromScale(0.5, 0.45)
TweenService:Create(frame, info1, {Position = UDim2.fromScale(0.5, 0.5)}):Play()
TweenService:Create(Camera, info1, {FieldOfView = 80}):Play()
TweenService:Create(Blur, info1, {Size = 9}):Play()
else
CloseFrames()
end
end
for _, button in Buttons:GetDescendants() do
if button:IsA("GuiButton") then
button.MouseButton1Click:Connect(function()
OpenFrame(Frames[button.Name])
end)
end
end
for _, frame in Frames:GetChildren() do
frame.Close.MouseButton1Click:Connect(function()
CloseFrames()
end)
end
The issue is most likely that you are only running these once. I would recommend putting them in a RunService loop:
local rns = game:GetService("RunService")
rns.Heartbeat:Connect(function()
for _, button in Buttons:GetDescendants() do
if button:IsA("GuiButton") then
button.MouseButton1Click:Connect(function()
OpenFrame(Frames[button.Name])
end)
end
end
for _, frame in Frames:GetChildren() do
frame.Close.MouseButton1Click:Connect(function()
CloseFrames()
end)
end
end)
Why would OP need to make connections every frame?
the problem is that the frame remains visible and the rest of the functions are performed
The way OP has the script set up is that they have a for loop going through every button and checking if it is clicked. However, the for loops only run once (at the startup of the script). If he has the for loops check every frame, the buttons should work as normal. If you have a way that it could be optimized, I and probably OP would love to know.
No they’re going through every button and connecting events to them.
The for loops only check through every button one time.
My buttons work, but the frame does not become invisible
I just tested the script and everything seems to be working fine actually. Can I see what your startergui hierarchy looks like?
Do the if statement for the Tweens, but keep the frame.Visible = false code outside of the if statement in the CloseFrames function
This is absolutely unnecessary. If there isn’t buttons randomly cloning/appearing after going thru loops, there’s no point checking and connecting events multiple times per second.
That was my bad, I misunderstood how the script works and I take full responsibility for that.
Update above to this:
local function CloseFrames()
for _, frame in pairs(Frames:GetChildren()) do
if frame.Visible then
TweenService:Create(Camera, info1, {FieldOfView = 70}):Play()
TweenService:Create(Blur, info1, {Size = 0}):Play()
frame.Visible = false
end
end
end
that’s the problem
that’s the problem
The before and after do the same thing. Generalized iteration is valid.
I usually add pairs()
or ipairs()
in for loops, but it’s not really revelent here.
What about checking if the values ever reach the desired numbers?, like checking FOV actually gets to 70. I usually have issues with rounding the FOV and never get exactly 70, but something like 69.00999995566
Absolutely no idea what can be issue
local function CloseFrames()
for _, frame in Frames:GetChildren() do
if frame.Visible then
TweenService:Create(Camera, info1, {FieldOfView = 70}):Play()
TweenService:Create(Blur, info1, {Size = 0}):Play()
frame.Visible = false
end
end
end
for _, frame in Frames:GetChildren() do
frame.Close.MouseButton1Click:Connect(function()
CloseFrames()
end)
end
Is there any reason why you are using a for loop
instead of toggling the Inventory frame directly?