Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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?

2 comments

0

Are you using a local script or a script?

0

Local script in a starter pack

2 answers

✓ Accepted answer

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)

2 comments

0

Nice job!

0

Ty!

Creating a variable called target, then asking if target then

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

This discussion is preserved as it was. New answers, comments, and votes are closed.