I want to make a GUI element that would show up the name of the object the player is currently looking at. The script works but not as I intended to:
I need to make the name of the object disappear if the player isn’t currently looking at it. I can’t understand what I am doing wrong here:
local players = game:GetService("Players")
local char = players.LocalPlayer.Character
local root = char:WaitForChild("HumanoidRootPart")
local cam = workspace.CurrentCamera
local uis = game:GetService("UserInputService")
local label = players.LocalPlayer:WaitForChild("PlayerGui"):WaitForChild("guistuff"):WaitForChild("WhatYouLookinAt")
local runservice = game:GetService("RunService")
local raycastParams = RaycastParams.new()
raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
raycastParams.FilterDescendantsInstances = {players.LocalPlayer.Character, players, workspace.Baseplate, workspace.mapstuff}
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
if result.Instance.Name ~= label.Text then
label.Text = nil
end
end
end)