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

Gui Script| Script Not Changing?

Asked by 8 years ago

Idea Create a part, and focus the camera on it to use it as a gui background
Problem The script runs and does it fine, but the issue is that the gui doesn't change when a Bool value is changed in workspace? any ideas?
Possibility I think that I need to use a changed function, but I'm not sure how to set that up in this script.
Code

--------------------------------------------------------
-- Setup
--------------------------------------------------------

self = script.Parent
c = workspace.CurrentCamera
rs = game:GetService("RunService")
util = LoadLibrary("RbxUtility")
Create = util.Create;
music = self:WaitForChild("Music")

music:Stop()
music:Play()

game.StarterGui:SetCoreGuiEnabled("PlayerList",false)
game.StarterGui:SetCoreGuiEnabled("Health",false)
game.StarterGui:SetCoreGuiEnabled("Backpack",false)

---------------------------------------------------------
-- Screen Utilities

function ScreenSpaceToWorld(x, y, depth)
    local resolution = self.AbsoluteSize
    local aspectRatio = resolution.X / resolution.Y
    local hfactor = math.tan(math.rad(c.FieldOfView)/2)
    local wfactor = aspectRatio*hfactor
    local xf, yf = x/resolution.X*2 - 1, y/resolution.Y*2 - 1
    local xpos = xf * -wfactor * depth
    local ypos = yf *  hfactor * depth
    return Vector3.new(xpos, ypos, depth)
end

function GetDepthForWidth(partWidth, visibleSize)
    local resolution = self.AbsoluteSize;
    local aspectRatio = resolution.X / resolution.Y
    local hfactor = math.tan(math.rad(c.FieldOfView)/2)
    local wfactor = aspectRatio*hfactor
    return (-0.5*resolution.X*partWidth/(visibleSize*wfactor))
end

---------------------------------------------------------------




----------------------------------------------------------------
-- Build backdrop

local backdrop = Create'Part'
{
    FormFactor = "Custom";
    Anchored = true;
    CanCollide = false;
    TopSurface = "Smooth";
    BottomSurface = "Smooth";
    Material = "Neon";
    BrickColor = BrickColor.new("Navy blue");
    Parent = c;
}


function updateBackdrop()
    local x,y = self.AbsoluteSize.X,self.AbsoluteSize.Y
    local cx,cy = math.floor(x/2),math.floor(y/2)
    local cz = GetDepthForWidth(40,x)
    local ratio = y/x
    local pos = ScreenSpaceToWorld(cx,cy,cz)
    backdrop.Size = Vector3.new(40,40*ratio,0)
    backdrop.CFrame = c.CoordinateFrame * CFrame.new(pos)
    if game.Workspace.v.Value == false then
    c.CameraType = "Scriptable"
    c.CoordinateFrame = CFrame.new(9999,9999,9999)
    if game.Workspace.v.Value == true then
    c.CameraType = "Follow"
    end
    end
    end

updateBackdrop()
rs.RenderStepped:connect(updateBackdrop)

-------------------------------------------------------------------

-------------------------------------------------------------------

Thanks :D

1 answer

Log in to vote
0
Answered by 8 years ago

The changed event would work like this:

updateBackdrop() --First time setup

Workspace.v.Changed:connect(function() --Will be called if the value changes
    updateBackdrop()
end)

You will have to create another function for the actual camera movement.

This might work?

--------------------------------------------------------
-- Setup
--------------------------------------------------------

self = script.Parent
c = workspace.CurrentCamera
rs = game:GetService("RunService")
util = LoadLibrary("RbxUtility")
Create = util.Create;
music = self:WaitForChild("Music")

music:Stop()
music:Play()

game.StarterGui:SetCoreGuiEnabled("PlayerList",false)
game.StarterGui:SetCoreGuiEnabled("Health",false)
game.StarterGui:SetCoreGuiEnabled("Backpack",false)

---------------------------------------------------------
-- Screen Utilities

function ScreenSpaceToWorld(x, y, depth)
    local resolution = self.AbsoluteSize
    local aspectRatio = resolution.X / resolution.Y
    local hfactor = math.tan(math.rad(c.FieldOfView)/2)
    local wfactor = aspectRatio*hfactor
    local xf, yf = x/resolution.X*2 - 1, y/resolution.Y*2 - 1
    local xpos = xf * -wfactor * depth
    local ypos = yf *  hfactor * depth
    return Vector3.new(xpos, ypos, depth)
end

function GetDepthForWidth(partWidth, visibleSize)
    local resolution = self.AbsoluteSize;
    local aspectRatio = resolution.X / resolution.Y
    local hfactor = math.tan(math.rad(c.FieldOfView)/2)
    local wfactor = aspectRatio*hfactor
    return (-0.5*resolution.X*partWidth/(visibleSize*wfactor))
end

---------------------------------------------------------------




----------------------------------------------------------------
-- Build backdrop

local backdrop = Create'Part'
{
    FormFactor = "Custom";
    Anchored = true;
    CanCollide = false;
    TopSurface = "Smooth";
    BottomSurface = "Smooth";
    Material = "Neon";
    BrickColor = BrickColor.new("Navy blue");
    Parent = c;
}


function updateBackdrop()
    if game.Workspace.v.Value == false then
    c.CameraType = "Scriptable"
    c.CoordinateFrame = CFrame.new(9999,9999,9999)
    elseif game.Workspace.v.Value == true then
    c.CameraType = "Follow"
    end
end

updateCamera = function()
    if c.CameraType == "Follow" then
    local x,y = self.AbsoluteSize.X,self.AbsoluteSize.Y
    local cx,cy = math.floor(x/2),math.floor(y/2)
    local cz = GetDepthForWidth(40,x)
    local ratio = y/x
    local pos = ScreenSpaceToWorld(cx,cy,cz)
    backdrop.Size = Vector3.new(40,40*ratio,0)
    backdrop.CFrame = c.CoordinateFrame * CFrame.new(pos)
    end
end

updateBackdrop()

workspace.v.Changed:connect(function()
    updateBackdrop()
end

rs.RenderStepped:connect(updateCamera)
-------------------------------------------------------------------
0
im a tiny bit confused on how i would incorperare that, i tried and it didnt work, can i a bit more help? Thanks :D pluginfactory 463 — 8y
0
Would would need to handle the camera movement stuff in it's own function (called by the RenderStepped event), and leave the if statement stuff (line 70-75) in the current function. darkelementallord 686 — 8y
0
You also messed up your if statements in the updateBackdrop function. The value would have to be false, and true to make the cameratype "follow" darkelementallord 686 — 8y
0
im getting an error: Players.Player.PlayerGui.ScreenGui.LocalScript:89: ')' expected (to close '(' at line 85) near 'rs' pluginfactory 463 — 8y
Ad

Answer this question