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

Can you use Changed when the global is nil?

Asked by
LostPast 253 Moderation Voter
9 years ago
repeat wait() until _G.PlayerBall ~= nil
_G.PlayerBall.Changed:connect(function()
    if _G.PlayerBall ~= nil then
        Ball = _G.PlayerBall
    elseif _G.PlayerBall == nil then
        Ball = nil
    end
end)

On line 2 it errors because I have _G.PlayerBall as a nil value every once in a while on purpose. Is there a way to overlook this so that it runs and doesn't error?

Thank you for your help!

0
Changed isn't a thing for all objects in Lua -- only ROBLOX instances get it. If `_G.PlayerBall` is just a variable you're controlling, it doesn't have a `Changed`. If it's a ObjectValue or StringValue, etc, then see Redbullusa's answer BlueTaslem 18071 — 9y
0
Changed actually works on _G.(Globals) as long as they aren't nil. LostPast 253 — 9y

1 answer

Log in to vote
0
Answered by
Redbullusa 1580 Moderation Voter
9 years ago

The .Changed event only fires if the property of that object changes, not the existence of the object itself. Rather than doing the latter, try and see if the property that was changed is the Parent. Then see if the Parent of the object is nil.

-- Now that I think about it, this is pretty irrelevant to your question. This only applies if the object is not nil, but the Parent of it is.
_G.PlayerBall.Changed:connect(function(property)
    if property == "Parent" then
        if not _G.PlayerBall.Parent then
            Ball = nil
        else
            Ball = _G.PlayerBall
        end
    end
end)

-- If the script still errors, then this most likely does not apply to you. Check out the links below if that happens.

Now, regarding deflection of errors, check out these links:

0
Changed actually works on _G.(Globals) as long as they aren't nil. Please remove your answer because it isn't relevant as you so said above. LostPast 253 — 9y
0
That's what I said on the first line. Redbullusa 1580 — 9y
0
oh sorry then XD LostPast 253 — 9y
Ad

Answer this question