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

2 comments

0

Maybe make a variable for CurrentCamera so the script doesn't have to find it in workspace over and over again.

0

Perhaps you should use RunService.RenderStepped instead of a while loop.