I am currently developing a plugin that relies on game.Selection
changes to operate, but I am running into a few issues. Here are the methods I've tried:
Change events.
function newSelection() -----Implementation not shown----- end game.Selection.Changed:connect(newSelection)
This method does not throw any compiler errors, but it never responds when I change my selection. I can only be led to believe this is caused by the fact that the .Changed
event only responds when the actual game.Selection
object changes, not the game.Selection:Get()
.
Looping if statements.
local oldSelection=nil function newSelection() -----Implementation not shown----- end while wait(0.25) do if game.Selection:Get()[1]~=oldSelection then oldSelection=game.Selection:Get()[1] end end
This style works and has no compiler errors, but it still has two issues: -There is a short but noticeable delay when the selection changes. -Depending on the size of the game, the loop will slow the game down.
Does anybody have any other methods of a change detection? Thanks,
ChipioIndustries
There is a SelectionChanged event of the Selection service
local selection = game:GetService("Selection") selection.SelectionChanged:connect(function() print'Selection changed!' end)
or assuming you are doing this in a local script you can use a loop connected to renderstep (Not recommended but you can and you won't see a delay)
local rs = game:GetService("RunService") rs.RenderStepped:connect(function() -- CHECK SELECTION end)