Scripts within StarterCharacterScripts nested within Folders run when they shouldn't

Scripts within StarterCharacterScripts (the server-side service instance) shouldn’t run, because they’re reserved to run only when they are cloned into a Character. This is expected, this is good! The problem is, if a script inside of StarterCharacterScripts is placed inside a Folder for organising reasons such as below:
image
It will erroneously execute inside of StarterCharacterScripts when it should not.
Studio output:
image

While this specific instance it does not cause any tangible problem aside from adding clutter to the output each session (the WaitForChild stops it from progressing forward in the script any further), in other script circumstances it could cause chaos.

2 Likes

Hello ZacAttackk,
Would it be possible for you to share the contents of CharacterMain up to the error on line 21? Or at the very least, the lines that are relevant to instantiating what is being called on line 21? Also, to run into this error, you essentially just press Play while having your script inside a folder inside of StarterCharacterScripts, correct?

Hello! Apologies for not attaching a repro file. Here’s a new one now demonstrating the issue:

CharacterScriptsRunIssue.rbxl (53.3 KB)

image

Beta features enabled:
All of them aside from New Luau type solver, Next Gen Studio preview, Scripts are non-strict by default & Texture Generator

Hello ZacAttackk,
The reason why you are getting this error is that you are using Scripts for writing this code inside of StarterCharacterScripts, and not LocalScripts. LocalScripts are designed to run on the player client. The error printout you are getting is when the DataModel in Play Mode is cloning the script into the StarterCharacterScripts folder that exists as a service and runs the script as a result. If you replace the Scripts in this folder with LocalScripts, you should no longer get this error.

These specific scripts are designed to run server-side by being automatically inserted to every player character, so that is not the correct option for me in this instance. I feel like this is unintended behaviour, because Server Scripts that are descendants of any data model aside from Workspace and ServerScriptService purposefully do not execute.

This includes StarterCharacterScripts, as a server-sided script that is a direct child of this service does not execute, whereas if you put the same server script inside a Folder that is a child of this service, it then DOES execute.

As stated it is not a big issue for me at all, but could potentially be for other developers. A current workaround is to simply check the Folder parent’s type and return to stop the rest of the script from executing:

local scbin = script.Parent
local c: Model = scbin.Parent
if c:IsA("StarterCharacterScripts") then -- Error reported: https://devforum.roblox.com/t/scripts-within-startercharacterscripts-nested-within-folders-run-when-they-shouldnt/3164722
	return -- this should cancel the rest of the entire script from running.
end

This has to do with your script’s RunContext being set to Server, which when the RunContext is set to non-Legacy the script is just allowed to run… anywhere. Which seems to be intended behavior unfortunately, given the FAQ section of the announcement post for RunContext:

1 Like

As mentioned, this is intentional as we want the behavior of scripts using RunContext to be consistent. I’d recommend the following workaround:

if not script:FindFirstAncestorWhichIsA("Player") then
  return
end
1 Like

Ah I see, my RunContext was set to server. Setting it to Legacy showed that the script no longer executed. Sorry for the confusion!

2 Likes

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