Here's the script:
local Names = {"Tree","Apple","Bench","Bridge"} local Player = game.Players.LocalPlayer local Mouse = Player:GetMouse() local ScreenGui = Player.PlayerGui:WaitForChild("ObjectGui") local Title = ScreenGui:WaitForChild("Background"):WaitForChild("Title") local Desc = ScreenGui:WaitForChild("Background"):WaitForChild("Description") game:GetService('RunService').RenderStepped:connect(function() local Y = Mouse.Y local X = Mouse.X if Mouse.Target then if Mouse.Target ~= nil then for i,v in pairs (Names) do if Mouse.Target.Parent.Name then if Mouse.Target.Parent.Name == v then --Error here: attempt to index field 'Target' (a nil value) ScreenGui:WaitForChild("Background").Visible = true ScreenGui:WaitForChild("Background").Position = UDim2.new(0,X,0,Y) Title.Text = v Desc.Text = Mouse.Target.Parent:WaitForChild("Description").Value wait(.5) ScreenGui:WaitForChild("Background").Visible = false else ScreenGui:WaitForChild("Background").Visible = false end end end end end end)
I tried to do if Mouse.Target ~= nil,If Target ~= nil,pcall(),if Mouse.Target.Name. And none of these worked for some reason. When I hover my mouse over the sky of the game, it makes the gui dissapear how it is supposed to, but it SPAMS a massive error message on the output saying that Mouse.Target is nil. How do I stop that? Am i missing something?
The reason for the errors is that inside the 'if' statement where you verify that Mouse.Target exists, you have a loop that uses Mouse.Target, but which takes 2 seconds to run (4 * wait(0.5)). If you move your mouse into the sky within that 2 seconds, Mouse.Target will now be nil, but your loop is still using it, hence the errors. Instead of using a wait, it would be better to keep track of how long the mouse has been off of objects parented to your named objects, and then set gui visibility to false once it gets over a certain time. I have modified your code below to give you an example. (I also pulled the mouse check out into it's own function just to keep the code cleaner)
local lastOnTarget = 0 -- Edit this variable to determine how fast or slow the gui hides local guiHideTime = 0.5 local function CheckMouseTarget() if Mouse.Target then local targetName = Mouse.Target.Parent.Name for i = 1, 4 do if targetName == Names[i] then return true end end end return false end game:GetService('RunService').RenderStepped:connect(function() if CheckMouseTarget() then if not isVisible then -- Set Gui visible here isVisible = true end lastOnTarget = tick() elseif isVisible then if tick() - lastOnTarget > guiHideTime then -- Set Gui invisible here isVisible = false end end end)
Creating a variable called target, then asking if target then
local target = mouse.Target if target then --Bla bla bla end