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

How to stop GUI from appearing when you don't look at the Sun in this script?

Asked by 2 years ago
local s = script.Parent
local gui = script.Parent.Parent
s.Visible = true

local plry = game.Players.LocalPlayer
local char = plry.Character or plry.CharacterAdded:Wait()

local head = char:FindFirstChild("Head") or char:WaitForChild("Head")

local rs = game:GetService("RunService")

rs.RenderStepped:Connect(function()
    local cam = workspace.CurrentCamera
    local lighting = game.Lighting
    local sundir = cam.CFrame.p + lighting:GetSunDirection()

    local screenpos = cam:WorldToScreenPoint(sundir)

    s.Position = UDim2.new(0,screenpos.X,0,screenpos.Y)

    local bupleft = 0 - s.AbsoluteSize.Y
    local bupright = 0 - s.AbsoluteSize.X
    local bdownleft = gui.AbsoluteSize.Y + s.AbsoluteSize.Y
    local bdownright = gui.AbsoluteSize.X + s.AbsoluteSize.X

    local rayprams = RaycastParams.new()
    rayprams.FilterType = Enum.RaycastFilterType.Blacklist
    rayprams.FilterDescendantsInstances = {char}
    local raydir = lighting:GetSunDirection() * 99999
    local raycast = workspace:Raycast(cam.CFrame.Position, raydir, rayprams)

    local ts = game:GetService("TweenService")
    local ti = TweenInfo.new(0.05, Enum.EasingStyle.Linear)
    if raycast and s.AbsolutePosition.X >= (bupleft or bupright) and s.AbsolutePosition.Y <= (bdownleft or bdownright) then
        ts:Create(s, TweenInfo.new(0.01, Enum.EasingStyle.Linear), {Size = UDim2.new(0,0,0,0)}):Play()
    else
        ts:Create(s, ti, {Size = UDim2.new(0,850,0,850)}):Play()
    end
end)    

Everything else works perfectly, but the problem is: when I look at the direction opposite of the Sun's, the GUI appears. I kinda figured out why, but idk how to fix.

1 answer

Log in to vote
0
Answered by
Xapelize 2658 Moderation Voter Community Moderator
2 years ago
Edited 2 years ago

before setting the frame's position, check if the screenpos.Z is over 0, I experimented for a while and notice if you look at the opposite of the sun, the frame will appear but the screenpos.Z will be under 0, but if you look at the sun the screenpos.Z will be over 0. Hope this helps

Code:

local s = script.Parent
local gui = script.Parent.Parent
s.Visible = false

local plry = game.Players.LocalPlayer
local char = plry.Character or plry.CharacterAdded:Wait()

local head = char:FindFirstChild("Head") or char:WaitForChild("Head")

local rs = game:GetService("RunService")

rs.RenderStepped:Connect(function()
    local cam = workspace.CurrentCamera
    local lighting = game.Lighting
    local sundir = cam.CFrame.p + lighting:GetSunDirection()

    local screenpos = cam:WorldToScreenPoint(sundir)

    if not (screenpos.Z > 0) then
        s.Visible = false
        return
    end
    s.Visible = true

    s.Position = UDim2.new(0,screenpos.X,0,screenpos.Y)

    local bupleft = 0 - s.AbsoluteSize.Y
    local bupright = 0 - s.AbsoluteSize.X
    local bdownleft = gui.AbsoluteSize.Y + s.AbsoluteSize.Y
    local bdownright = gui.AbsoluteSize.X + s.AbsoluteSize.X

    local rayprams = RaycastParams.new()
    rayprams.FilterType = Enum.RaycastFilterType.Blacklist
    rayprams.FilterDescendantsInstances = {char}
    local raydir = lighting:GetSunDirection() * 99999
    local raycast = workspace:Raycast(cam.CFrame.Position, raydir, rayprams)

    local ts = game:GetService("TweenService")
    local ti = TweenInfo.new(0.05, Enum.EasingStyle.Linear)
    if raycast and screenpos.Z < 0 and s.AbsolutePosition.X >= (bupleft or bupright) and s.AbsolutePosition.Y <= (bdownleft or bdownright) then
        ts:Create(s, TweenInfo.new(0.01, Enum.EasingStyle.Linear), {Size = UDim2.new(0,0,0,0)}):Play()
    else
        ts:Create(s, ti, {Size = UDim2.new(0,50,0,50)}):Play()
    end
end)   
0
I just replaced your changes with a simple if-then statement. It worked fine now. Thanks for the idea! RazorXX2 24 — 2y
Ad

Answer this question