Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How to add a GUI when camera is Y <= 0? [Not efficient enough]

Asked by 7 years ago
Edited 7 years ago

I'm trying to create a script, when you are below the water, a blue GUI appears. The issue is that this script doesn't seem very efficient. I've tried this with the CurrentCamera, the Changed function, but it did nothing and I didn't find any other function. Is there a way to reduce the lag of this script?

while true do
    wait(0.3) -- To reduce intense lag
    if game.Workspace.CurrentCamera.CFrame.Y <= 0 then
        if game.Players.LocalPlayer.PlayerGui.ScreenGui:FindFirstChild('WaterEffect') then
            print('GUI exists while the player is underwater, all good!')
        else
            print("GUI doesn't exist while the player is underwater and should be, creating a GUI..")
            local waterEffect = Instance.new('Frame', game.Players.LocalPlayer.PlayerGui.ScreenGui)
            waterEffect.Name = 'WaterEffect'
            waterEffect.Size = UDim2.new(1,0,1,0)
            waterEffect.BackgroundColor3 = Color3.new(0,0,1)
            waterEffect.BackgroundTransparency = .7
        end
    else
        if not game.Players.LocalPlayer.PlayerGui.ScreenGui:FindFirstChild('WaterEffect') then
            print('No GUI while the player is the air, all good!')
        else
            print('The player is in the air and the GUI needs to be removed.')
            game.Players.LocalPlayer.PlayerGui.ScreenGui:FindFirstChild('WaterEffect'):Destroy()
        end
    end
end

Thanks! :)

Edit 2: Fixed grammar in the print()'s

0
Maybe make a variable for CurrentCamera so the script doesn't have to find it in workspace over and over again. User#11440 120 — 7y
0
Perhaps you should use RunService.RenderStepped instead of a while loop. ShadowsDev 62 — 7y

Answer this question