I want to make my text label to fade in and stay like that while player is looking at it, but can’t get it to work. If I put tween reversing off it just fades in one time and then it stops tweening completely. Here’s a video:
local tweenservice = game:GetService("TweenService")
local tinfo = TweenInfo.new(0.4, Enum.EasingStyle.Quart, Enum.EasingDirection.InOut, 0, true, 0)
local tweener = tweenservice:Create(label ,tinfo, {TextTransparency=0, TextStrokeTransparency=0})
local runservice = game:GetService("RunService")
local label = players.LocalPlayer:WaitForChild("PlayerGui"):WaitForChild("guistuff"):WaitForChild("WhatYouLookinAt"
local cam = workspace.CurrentCamera
local uis = game:GetService("UserInputService")
local function MouseRaycast()
local mousePos = uis:GetMouseLocation()
local mouseRay = cam:ViewportPointToRay(mousePos.X, mousePos.Y)
local rayResult = workspace:Raycast(mouseRay.Origin, mouseRay.Direction * 20, raycastParams)
return rayResult
end
runservice.Heartbeat:Connect(function()
local result = MouseRaycast()
if result and result.Instance then
tweener:Play()
label.Text = result.Instance.Name
if result.Instance.Name ~= label.Text then
label.Text = ""
end
else
label.Text = ""
end
end)
I think your fading problem may be because you are playing the tween every “Heartbeat”.
Try checking if the players target has changed. If the target has changed, then play the tween.
Set a fade in tween to have no repeats and create a fade out tween which you would run whenever the text should be removed. It’ll potentially fix your problem.
local tweenservice = game:GetService("TweenService")
local tinfo = TweenInfo.new(0.4, Enum.EasingStyle.Quart, Enum.EasingDirection.InOut, 0, false, 0)
local tweener = tweenservice:Create(label ,tinfo, {TextTransparency=0, TextStrokeTransparency=0})
local TweenOut = tweenservice:Create(label ,tinfo, {TextTransparency=1, TextStrokeTransparency=1})
local runservice = game:GetService("RunService")
local label = players.LocalPlayer:WaitForChild("PlayerGui"):WaitForChild("guistuff"):WaitForChild("WhatYouLookinAt"
local cam = workspace.CurrentCamera
local uis = game:GetService("UserInputService")
local function MouseRaycast()
local mousePos = uis:GetMouseLocation()
local mouseRay = cam:ViewportPointToRay(mousePos.X, mousePos.Y)
local rayResult = workspace:Raycast(mouseRay.Origin, mouseRay.Direction * 20, raycastParams)
return rayResult
end
runservice.Heartbeat:Connect(function()
local result = MouseRaycast()
if result and result.Instance then
tweener:Play()
label.Text = result.Instance.Name
if result.Instance.Name ~= label.Text then
tweener:Stop()
TweenOut:Play()
label.Text = ""
end
else
label.Text = ""
TweenOut:Play()
end
end)
Adjust the code optimization further up, this one will not be efficient. Reference I provided was a potential concept of solving your problem rather than the solution itself.
I changed the tween according to how it should be seen (function Tween(x)) and added the condition (if label.Text ~= “” then) that when I move the mouse to “nil” so that the animation runs smoothly.
--Service
local players = game:GetService("Players").LocalPlayer
local tweenservice = game:GetService("TweenService")
local runservice = game:GetService("RunService")
local uis = game:GetService("UserInputService")
--Gui
local label = players:WaitForChild("PlayerGui"):WaitForChild("guistuff"):WaitForChild("WhatYouLookinAt")
--Workspace
local cam = workspace.CurrentCamera
--Values
local raycastParams = RaycastParams.new()
function Tween(x)
local tinfo = TweenInfo.new(0.4, Enum.EasingStyle.Quart, Enum.EasingDirection.Out, x, true, 0)
local tweener = tweenservice:Create(label ,tinfo, {TextTransparency=x, TextStrokeTransparency=x})
tweener:Play()
end
local function MouseRaycast()
local mousePos = uis:GetMouseLocation()
local mouseRay = cam:ViewportPointToRay(mousePos.X, mousePos.Y)
local rayResult = workspace:Raycast(mouseRay.Origin, mouseRay.Direction * 20, raycastParams)
return rayResult
end
runservice.Heartbeat:Connect(function()
local result = MouseRaycast()
if result and result.Instance then
label.Text = result.Instance.Name
Tween(0)
else
if label.Text ~= "" then
Tween(1)
task.wait(0.4)
label.Text = ""
end
end
end)