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

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

Asked by
RoyMer 301 Moderation Voter
7 years ago

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)
0
is this in a local script in a storage that replicates over every client? Angels_Develop 52 — 7y
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) RoyMer 301 — 7y

1 answer

Log in to vote
1
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
7 years ago
Edited 7 years ago

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).

0
Thanks, that was it! RoyMer 301 — 7y
Ad

Answer this question