I'm trying to make it so when a player walks into a specific region in the game, they will receive a GUI, although the player does obtain the GUI, it spams it multiple times. I've tried to check if the GUI is present, but I'm not 100% sure how to do that, any help would be great! Here's the script:
local Player = game.Players.LocalPlayer
repeat wait() until Player.Character
local Char = Player.Character
local RootPart = Char:WaitForChild("HumanoidRootPart")
local GUI = game.ReplicatedStorage.AIKeyboard
while wait() do
local hit = workspace:FindPartOnRay(Ray.new(RootPart.Position, Vector3.new(0, -20, 0)), Char)
if hit then
if hit.Name == "Region" then
for i,v in pairs(Player.PlayerGui:GetChildren()) do
if v:IsA("ScreenGui") and v.Name == "AIKeyboard" then
if v.Name == "AIKeyboard" then
print("Keyboard is present")
elseif v.Name ~= "AIKeyboard" then
GUI:Clone().Parent = game.Players.LocalPlayer.PlayerGui
end
end
end
else
for i,v in pairs(Player.PlayerGui:GetChildren()) do
if v:IsA("ScreenGui") and v.Name == "AIKeyboard" then
v:Destroy()
end
end
end
end
end

4 comments
Do you even know why "while wait() do" works, why are you using it, why not use something else, and please fix the indenting, and don't repeat wait() until Player.Character. That is stupid. Use events. Specifically, Player.CharacterAdded
I know what while wait() does, and I tried using other things than that but it doesn't work. And it is a local script, repeat wait() works for that and I see others using it, so where's your point there? No need to be insult someone over a question they're asking. Instead of saying "that's stupid" tell me exactly what to use. Most questions I've asked you've left small remarks that don't help at a
repeat wait() is an old implementation for what you are trying to do before Player.CharacterAdded event was added. Use the events because they are always going to be more efficient than your lua code.
"And it is a local script, repeat wait() works for that and I see others using it, so where's your point there?" is an appeal to popularity (https://yourlogicalfallacyis.com/bandwagon), and is not a logically sound argument for following some certain idiom. Millions of flies eat feces, does that mean feces are good? No. Same goes for the
while wait() doidiom. Usewhile true do .. end. It's cl