Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Gui Script| Script Not Changing?

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

✓ Accepted answer

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)
-------------------------------------------------------------------

4 comments

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

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.

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"

0

im getting an error: Players.Player.PlayerGui.ScreenGui.LocalScript:89: ')' expected (to close '(' at line 85) near 'rs'

This discussion is preserved as it was. New answers, comments, and votes are closed.