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

Can you use Changed when the global is nil?

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!

2 comments

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

0

Changed actually works on _G.(Globals) as long as they aren't nil.

1 answer

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:

3 comments

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.

0

That's what I said on the first line.

0

oh sorry then XD

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