Wrong warning on Script Analysis

Reproduction Steps
All examples are described here: Wrong warning on Script Analysis

Expected Behavior
Incorrect warnings get in the way and distract from the correct warnings because if I get used to ignoring the incorrect warnings, I won’t pay attention to the correct ones.

Actual Behavior
All examples are described here: Wrong warning on Script Analysis

Workaround
No

Issue Area: Studio
Issue Type: Other
Impact: Moderate
Frequency: Constantly

1 Like

The warning is correct. To fix it, you need to add local c inside Players.PlayerAdded:Connect( function(Player).

6 Likes

I thought they said that didn’t work?

Please, test it.

1 Like

The global c isnt defined until function b is called. Observe the following:

local function a()
    function b()

    end
end

print(b) -- nil
a()
print(b) -- function: 0xa682575f9dec6b6d
1 Like

oh yea they never did a() or b() ur right, doesn’t explain my script analysis error tho, hmm

image

This code as written would call a nil, but your code would as well. I’m assuming the more complete example actually calls a&b before calling c?

4 Likes

Is the message consider changing it to local saying to convert c into an upvalue like you did, so that it can be used on line 12?
I believe @rogeriodec_games’s issue is that the warning
Global ‘c’ is only used in the enclosing function defined at line 2; consider changing it to local
doesn’t really make sense here, since the global c is used outside it’s enclosing function b, rather than the anonymous callback function for PlayerAdded (well, it’s used in both, but the issue is that it’s defined inside the scope of b and is trying to be used outside of it).

A more concise demonstration would be
image
where the script analysis tool fails to detect the error where b is not defined until a is called.
It does silence this error, though!
image

That’s separate though - we don’t do flow analysis like that for now, so variables that can sometimes be used in uninitialized state aren’t detected.

This is why the message points to the outer function. Suggestions for more understandable text are always welcome of course.

2 Likes

I get the warnings only when the functions are inside Players.PlayerAdded:Connect( function(Player).

local Players = game:GetService("Players")
Players.PlayerAdded:Connect( function(Player)
	local function a()
		local function b()
			function c() 
				-- anything...
			end
			c()
		end
	end
	c()
end)

W003: (5,13) Global ‘c’ is only used in the enclosing function defined at line 2; consider changing it to local

But if outside PlayerAdded:Connect

--local Players = game:GetService("Players")
--Players.PlayerAdded:Connect( function(Player)
	local function a()
		local function b()
			function c() 
				-- anything...
			end
			c()
		end
	end
	c()
--end)

No warnings…

Why?

Because in this case adding a local definition for c would need to happen at the top level, which is approximately equivalent to a local. Adding a local in this case doesn’t make code more robust - the reason why this warning exists is that if c references something like Player object you may see subtle issues in the code due to conflating state from multiple callback calls. When it’s at the top level, it doesn’t matter because the code is executed once, so we don’t warn.

1 Like

Ok. So it’s not a bug?
Should I close this topic?

It should stay up. It’s good that OP didn’t firstly assume that it was a bug, but rather user error, hence the first topic being in #help-and-feedback:scripting-support . It didn’t look like the replies were too helpful therefore had to assume it was a bug, luckily everything has been cleared up. Future readers may assume similar in the future therefore this topic should be available to them.

oh yea, you are right about that

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.