A way for local and regular scripts to tell if an object is selected / to call `selection:Get()`

I would like regular scripts to have access to game.Selection:Get() or have some other way to check if they are being selected by the user.
I am currently making a custom humanoid-style script and would like it to display some debug info (stuff like billboardguis showing where its ground distance checks are hitting
image
) when it’s selected but when it calls game.Selection:Get() I get the error The current identity (2) cannot Get (lacking permission 1).
Is there any reason that the Get() function is pluginsecurity? Could we have a game.Selection:IsSelected(instance)?

Edit: I explained it better farther down:

This makes little sense – why are you calling Selection:Get() outside plugin source? Surely you can just do what you want to do inside the plugin source? Selection shouldn’t carry over to in-game because it is not normally available during game play.

If your use case is to tag a certain unit to display debug data while the game is running, it’s probably more appropriate to do this via a CollectionService tag.

By using a CollectionService tag you can then have a module that shows the debug info for the tagged objects, this way all you need to do is make a plugin that tags the selected object. This also allows you to add tags in-game if you wish for debugging to work in there, and integrating it with your own selection system.

@buildthomas, @FieryEvent
I don’t think I got my use case across well and I’m not sure how using tags helps here. Allow me to re-explain:

In the screenshot I posted before for my custom character handler you can see some billboard guis showing where the ground checks (raycasts fired downward from the hips) hit. I don’t want these to show all the time but I also don’t want to have to do some sort of command bar nonsense to tag them in order to enable this debugging info.
What I would like is for my scripts that are inside the tortoise model to be able to tell when they or the tortoise is selected IN STUDIO so it, WHILE STILL IN STUDIO can display this info, and otherwise not display it at all. I don’t want to have to spend my time making custom plugins and scripts that do this, I would like to just add a few lines of code to make some basic debug info like so:

local isSelected = false
for i, selected in ipairs(game.Selection:Get()) do
	if selected == script or selected == script.Parent or selected == Character then
		isSelected = true
		break
	end
end
if isSelected then
--show debug info, like the billboards in the screenshot
else
--don't show debug info
end

I don’t want to make some external script in ServerScriptService that other scripts have to talk to and I really don’t want to make a plugin. I just want one script to tell me what it’s doing and to not tell me what it’s doing when I don’t need it.
I like debugging like this in other game engines (where you’re only shown the information when the item that needs debugging is selected) because it is simple and gives me a lot of control as a developer.

We offered a workaround for your use case, because we judge it makes little sense for code in a game to be aware of the tools that built it. This is same code that gets run in roblox servers and studio is intended to simulate them as much as it can. If that code starts checking it’s environment it loses scalability and increases maintenance. The game.Selection:Get() method you referred to is currently safe in it’s security context (plugins), changing security of methods can create security risks for developers, such as game code getting references to instances it shouldn’t such as plugins themselves, and increased maintenance. The burdens of such changes aren’t probably worth than you yourself making your use case adapt to a small (really) plugin (which we can help you build) to solve your debugging needs.

1 Like

In my experience with Unity, I have found a certain debug function very useful:
This function would draw a line between two points, or from a point in a direction for a specified distance (depending on which variant you used), but these lines were only visible in the scene view (sort of like the server view in Roblox) and not in the gameview (sort of like the client view in Roblox). They never required almost any work on my end to use. If I’m not mistaken it was just Debug.DrawLine() and Debug.DrawRay(). We don’t have anything like this in Roblox so I’ve made my own code but while I can (and am currently) making it only visible while in studio (because RunService:IsStudio() is a thing, making my game’s code aware of the tools that built it), I can’t (easily) prevent them from being visible if I’m in the client view and not the server view. My options to do this without having to go super far out of my way everytime I want something like this in a new project is to
A). put them in the currentcamera, except that’s plugin security for the server as well or
B). Make the debug info only visible if you have the object selected
I guess what I’d really like is some way for things to be only visible from the server view, again, without much hassle because having to make an entire plugin for debugging vs. calling Debug.DrawLine() in Unity.
I guess if you have any suggestions on how to easily prevent something from replicating from the server to the client while still being visible and rendered, that’s what I’m really looking for.
I was just willing to settle for the info only being visible if the object was selected in studio, that way it would never be visible in a published game, but could be easily seen in studio without everything that can give debug info flooding the screen with info because it was only visible if selected.

You should draw your debug info using HandleAdorment objects instead of parts, and that will fix your issue with them being drawn on the server or interacting with your raycasts / etc.LineHandleAdornment will give you the equivalent to Debug.DrawLine(). Box/Cone/Cylinder/ImageHandleAdornment are available as well for composing basically whatever debug annotations you want.

While HandleAdornments seem useful, they still don’t really help with my information crowding problem where I would like the debug info(in this case HandleAdornments) to only be visible while either selecting an object or while in server-view (during play solo, when you can switch between them, kind of like only being visible from the server window when testing with a local server and 1 player).
It seems that what I’m looking for isn’t possible in Roblox so I guess I’ll just have to make due. Thanks for the help though, I’ll be sure to use LineHandleAdornments in the future as they look very useful.
My problem wasn’t that my debug info was getting in the way of raycasts (I just used FindPartOnRayWithIgnoreList), my problem is that the screen was getting busy with too much information.

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