Tween GUI ImageTransparency based on player distance from origin? (Magnitude)

I am trying to create a Gui tween that increments based on player distance from a set point. The best example is from the experience called “your last thoughts at the end of time” https://www.roblox.com/games/5889471811/your-last-thoughts-at-the-end-of-time
where the further you get from the spawn the color correction gets more red and more saturated. I want to implement something similar as an out of bounds prevention, or just to prevent the player from wandering too far from the objective. The effect would be a gui in the form of “static” like from a tv or camera instead of a post processing effect. They used a radius as a sort of “safe zone” where the effect is not happening. I have a perfect circle radius of lets say 2000 (the exact number is 2030 but I could just change the math) and I want it so that when their distance from the origin is greater than that 2000 their gui transparency tweens down from 1 (fully invisible) to 0.6 (slightly visible) based on distance.


The graphic shows possibly the best explanation of what I mean.

local tweenInfo1 = TweenInfo.new(2, Enum.EasingStyle.Linear, Enum.EasingDirection.In, 0, false);
local tweenIn = TweenService:Create(gui, tweenInfo1, {ImageTransparency = 0.6})
local tweenOut = TweenService:Create(gui, tweenInfo1, {ImageTransparency = 1})
--distance 
wait(2) --load character
local Position1 = game.Players.LocalPlayer.Character.HumanoidRootPart.Position
local centerpart = workspace.centerpart.Position
game:GetService("RunService").Heartbeat:Connect(function()
	local magnitude = (Position1 - centerpart).Magnitude
	if magnitude >= 2030 then
		tweenIn:Play()
	elseif magnitude <= 2030 then
		tweenOut:Play()
	end
end)

I don’t think I’m on track with what I have so far. This script would only have it tween up to that desired transparency even if you weren’t moving further away and were on the 2001 stud distance. I would need an equation that takes the extra distance on top of that 2000 distance safe zone and does the math to know how much to tween by per stud of what my graphic showed which was 200. A problem I was having is that when I would print the magnitude it would just print the value I was at when loading in and repeat it for the rest of the games ticks instead of updating when it changes. Another slightly more abstract problem is that the player will spawn outside of the safe zone and the script needs to be enabled when entering that radius which could probably be done with a collision detection.

Is there a better way to approach this that I’m not seeing?

I am not sure if this is what you trying to do

Here is the code:

local players = game:GetService("Players")
local plr = players.LocalPlayer


local char = plr.Character
local centerPart = workspace.centerPart

--Gui
local Gui = script.Parent
local imageLabel = Gui.ImageLabel

--Var

local SafeDistance = 50 --Change to your distance
local PerStudValue = 0.005 --Lower transparency per stud
local DefaultImageTransparency = 1 --Transparency at start of the game

game:GetService("RunService").Heartbeat:Connect(function()
	
	local charPosition = char.HumanoidRootPart.Position
	local centerPosition = centerPart.Position
	
	local DistanceFromCenter = (charPosition - centerPosition).Magnitude
	
	print("Distance from center: " .. DistanceFromCenter)
	
	if DistanceFromCenter >= SafeDistance then
		
		local DistanceFromSafeZone = (DistanceFromCenter - SafeDistance)
		
		local incrementTransparency = DistanceFromSafeZone * PerStudValue
		
		local finalTransparency = DefaultImageTransparency - incrementTransparency
		
		imageLabel.ImageTransparency = finalTransparency
		
	else
		
	end
	
end)

And the reason why your magnitude stay same because you need to update position of variable in Heartbeat too you can ask if you have any question

and god “your last thoughts at the end of time” crashed my pc

1 Like

that is pretty much exactly what i was after thank you. the “your last thoughts at the end of time” game has a lot of messages pinned all over the place and its really intensive. i was getting single digit frames but i also had studio open when i was on it so most of my ram was being consumed. i got off for the night but when i get back on ill test it and mark it as the solution when done