So I have this script that checks for a GUI named Tattoo2, but it keeps checking for it and spams my output, any way around this?
game:GetService("RunService").Stepped:Connect(function()
local Player = game.Players.LocalPlayer
local ScreenGui = Player.PlayerGui:WaitForChild("Tattoo2")
local TargetObject = workspace.NPCs.WisesNPC.UpperTorso
if Player.Character:WaitForChild("Humanoid").MoveDirection.Magnitude > 0 and (TargetObject.Position - Player.Character.HumanoidRootPart.Position).magnitude > 10 and ScreenGui then
ScreenGui:Destroy()
ScreenGui = nil
game.Workspace.NPCs.WisesNPC.UpperTorso.AlreadyOpened.Value = false
end
end)
Could you not just use WaitForChild at the top and avoid a useless loop?
Otherwise, since you have no prints something is erroring. Locate the error and fix it. We can’t do anything if we don’t know what’s happening. Send the output
Having any loop that waits in a Stepped event is a big mistake, especially one that repeats wait() until an object is found. The UX will drop significantly because the script will be performing wait() until it finds the instance.
In this example, the frame would wait() until a GUI is found in the PlayerGui, which means that your screen could just be stuck there infinitely if the script does not automatically give you the GUI that it is waiting for.
To answer the OP’s question, if there is absolutely no need to have this function run every frame, I recommend just using a while PlayerGui:FindFirstChild("Tattoo2") do loop, as the loop will not interrupt each frame as badly and still can be functional in the same way.
If it’s a given gui (like being inserted into the PlayerGui) you can make it reusable with something like this instead:
local Player = game:GetService("Players").LocalPlayer
local PlayerGui = Player.PlayerGui
local TargetObject = workspace.NPCs.WisesNPC.UpperTorso
local TargetGuiName = "Tattoo2"
local function CheckTattooDist(guiObject)
if not guiObject.Name == TargetGuiName then return end
while PlayerGui:FindFirstChild(TargetGuiName) do wait()
if Player.Character.Humanoid.MoveDirection.Magnitude > 0
and (TargetObject.Position - Player.Character.HumanoidRootPart.Position).magnitude > 10 then
PlayerGui[TargetGuiName]:Destroy()
TargetObject.AlreadyOpened.Value = false
end
end
end
PlayerGui.ChildAdded:Connect(CheckTattooDist)