SelectionService returning Plugin script instead of selected object

Hi,
For some reason I am unable to get SelectionService to work correctly.

local toolbar = plugin:CreateToolbar("Stat Assist")
local startSSA = toolbar:CreateButton("Start", "Start the stat form", "rbxassetid://7326562317")
local SelectionService = game:GetService("Selection")

--[[ Widget Settings ]]
local widgetInfo = DockWidgetPluginGuiInfo.new(Enum.InitialDockState.Float,false,false,300,500,200,50)
local ssaWidget = plugin:CreateDockWidgetPluginGui ("Stardust Stat Assistant", widgetInfo)
ssaWidget.Title = "Stardust Stat Assistant"

--[[ Buttons ]]
local SelectedObject = SelectionService:Get()[1]

--[[ Menu Launch & Edit/Setup ]]--
startSSA.Click:Connect(function()
    warn(SelectedObject)
end)

Saving this script (as local plugin) by itself defaults the printed selection as itself, StardustStatAssistv1.0, so the issue is contained within this specific script.
If you would like to test, just copy the code and save as a new plugin. Make sure you select an object in the explorer and then press the “Start” button for the plugin.

Maybe I’m being a noob and the issue is really obvious, perhaps it’s more complicated.
Either way I would seriously appreciate some answers as I’m completely stuck on this issue and it’s preventing me from continuing the development of the plugin.

Thank you :slight_smile:

1 Like

Initially when the (plugin) script is created, your selection is the script. You may want to redefine it inside the event:

local toolbar = plugin:CreateToolbar("Stat Assist")
local startSSA = toolbar:CreateButton("Start", "Start the stat form", "rbxassetid://7326562317")
local SelectionService = game:GetService("Selection")

--[[ Widget Settings ]]
local widgetInfo = DockWidgetPluginGuiInfo.new(Enum.InitialDockState.Float,false,false,300,500,200,50)
local ssaWidget = plugin:CreateDockWidgetPluginGui ("Stardust Stat Assistant", widgetInfo)
ssaWidget.Title = "Stardust Stat Assistant"

]

--[[ Menu Launch & Edit/Setup ]]--
startSSA.Click:Connect(function()
local SelectedObject = SelectionService:Get()[1
    warn(SelectedObject)
end)

Hope this helps!:smile:

2 Likes

Thank you! :smile:
I knew it would be this simple…

2 Likes