What do I want to achieve? I want to make that static will appear slow when you are far from slender and static will appear fast when he is near you.
What is the issue? The Issue is that when slender is far from you, the static appears very fast instead of appearing very slow.
What solutions have you tried so far? I tried many solutions but all of them doesn’t working.
local TS = game:GetService("TweenService")
local player = game.Players.LocalPlayer
local character = player.Character
local HRP = character:WaitForChild("HumanoidRootPart")
local part = game.Workspace:WaitForChild("Slender")
local static = player.PlayerGui:WaitForChild("Static").ImageLabel
local slendersounds = script:FindFirstChild("SlenderSounds")
local slenderisvisible = workspace:WaitForChild("SlenderValues").SlenderIsVisible
local Health = workspace:WaitForChild("SlenderValues").PlayerHealth
local distance = (HRP.CFrame.Position - part.CFrame.Position).Magnitude
script.StaticSoundChange.Enabled = true
game:GetService("RunService").RenderStepped:Connect(function()
local distance = (workspace.CurrentCamera.CFrame.Position - part.CFrame.Position).Magnitude
if slenderisvisible.Value == true then
Health.Value = Health.Value - distance/200
static.ImageTransparency = Health.Value/100
else
Health.Value = Health.Value + distance/200
static.ImageTransparency = Health.Value/100
end
if Health.Value > 100 then
Health.Value = 100
elseif Health.Value < 0 then
Health.Value = 0
end
if static.ImageTransparency < 0.5 then
static.ImageTransparency = 0.5
end
end)
Let’s say that distance is 1000 and Health is 100. Health’s value will be 95 after the first statement (instead of typing two Health.Value statements you can also type Health.Value -= distance/200 btw). This will make the transparency 0.95, which is almost fully transparent. At the end of the script, this will get reset to 0.5, which I assume what you wanted as fast.
On the other hand if distance is 100, Health value will be 99.5, and if you divide this by 100 it’ll be 0.995.
Comparing a distance which is 10 times shorter, the difference in transparency is only 0.045 which is not a noticeable difference.
The solution would be using lower division values, and not using a value object to hold this variable is also a great change, even if it’s not related with the main problem.
Instead of /200 on distance if we had /20, the original example would result with 50 in long distance and 0.5 on short, which has a way bigger contrast, you could get these values in between 0-1 with some maths but I think you can use a better solution here.
If you don’t exactly need to use transparency, you can also put a task.wait() in an infinite loop (don’t use this inside of an event), the wait amount would change with distance divided by some amount, ideally also with math.clamp to avoid huge numbers and a check to not even appear in very long distances.
I would not call any of these good solutions for this problem, the ideal solution would be holding a variable for time intervals between showing the image and making it disappear, however reducing this time per frame and not even activating the function unless the player is in the range, but this may be a little bit more advanced than the previous two solutions.
Might sound like lots of words for a simple problem but just wanted to explain the main idea.