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

if ~= nil yet it's still firing sometimes?

if mouse.Target ~= nil then it continues yet sometimes even when mouse.Target == nil it's still continuing, resulting me with this output

14:13:47.457 - Players.Player1.PlayerGui.ScreenGui.LocalScript:8: attempt to index field 'Target' (a nil value) 14:13:47.458 - Stack Begin 14:13:47.460 - Script 'Players.Player1.PlayerGui.ScreenGui.LocalScript', Line 8 14:13:47.461 - Stack End

14:13:48.553 - Players.Player1.PlayerGui.ScreenGui.LocalScript:11: attempt to index field 'Target' (a nil value) 14:13:48.554 - Stack Begin 14:13:48.555 - Script 'Players.Player1.PlayerGui.ScreenGui.LocalScript', Line 11 14:13:48.556 - Stack End

This is ONLY occuring in Studio!

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()

--Labelling of the objects
game:GetService('RunService').RenderStepped:connect(function()
	local Sign = script.Parent.TextLabel
	if mouse.Target ~= nil then
		if mouse.Target.Name == "Stuff" then
			Sign.Position = UDim2.new(0,mouse.X+16,0,mouse.Y)
			Sign.Visible = true
			Sign.Text = mouse.Target.Parent.Name
		else
			Sign.Visible = false	
		end		
	else
		Sign.Visible = false
	end
end)

2 comments

0

is this in a local script in a storage that replicates over every client?

0

It is in a local script in the starter gui, works perfect in play mode, even in studio but in studio sometimes it gives the error (still works)

1 answer

✓ Accepted answer

I have had this problem before and was very confused.

I cannot remember the exact circumstances that lead to it, but mouse.Target can change underneath you (even without wait() or any other yields).

In order to proceed safely, you have to save it to a local variable:

local target = mouse.Target
if target then
	if target.Name == "Stuff" then
		....
		Sign.Text = target.Parent.Name

EDIT: How? -- I have been reminded one way that this is possible. The mouse is actually able to move during the function, because the input thread is handled separately from the thread with Lua. Each time you ask mouse., it consults the mouse (which is being updated simultaneously).

1 comment

0

Thanks, that was it!

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