Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Mouse.Target is nil, how do I stop that?

Asked by 6 years ago

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?

0
Are you using a local script or a script? iRexBot 147 — 6y
0
Local script in a starter pack wilsonsilva007 373 — 6y

2 answers

Log in to vote
1
Answered by 6 years ago
Edited 6 years ago

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)
0
Nice job! magicguy78942 238 — 6y
0
Ty! wilsonsilva007 373 — 6y
Ad
Log in to vote
0
Answered by 6 years ago

Creating a variable called target, then asking if target then

local target = mouse.Target
if target then
    --Bla bla bla
end

Answer this question