Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
2

What is the most efficient method for detecting changes in game.Selection?

Asked by 9 years ago

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.

1function newSelection()
2    -----Implementation not shown-----
3end
4 
5game.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.

01local oldSelection=nil
02 
03function newSelection()
04    -----Implementation not shown-----
05end
06 
07while wait(0.25) do
08    if game.Selection:Get()[1]~=oldSelection then
09        oldSelection=game.Selection:Get()[1]
10    end
11end

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

1
holy crap a question on SH that's actually good?? I must be dreaming. Too bad I don't know the answer. Perci1 4988 — 9y
0
Using the loop shouldn't matter if you're developing a plugin. RepeatLua 90 — 9y

1 answer

Log in to vote
1
Answered by 9 years ago

There is a SelectionChanged event of the Selection service

1local selection = game:GetService("Selection")
2 
3selection.SelectionChanged:connect(function()
4    print'Selection changed!'
5end)

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)

1local rs = game:GetService("RunService")
2 
3rs.RenderStepped:connect(function()
4    -- CHECK SELECTION
5end)
Ad

Answer this question