Yesterday i was making a tutorial video about making a plugin and when I used ChangeHistoryService. it didn’t work for some reason.
Here is the video:
Here is the code:
local SelectionService = game:GetService("Selection")
local ChangeHistoryService = game:GetService("ChangeHistoryService")
local Toolbar = plugin:CreateToolbar("ToolboxCleaner")
local CleanScriptsButton = Toolbar:CreateButton("CleanScripts", "Select a model to clean all it's descendant scripts", "rbxassetid://3610247188")
local function CleanScriptsButtonClicked()
local Selection = SelectionService:Get()
for i, SelectedInstance in pairs(Selection) do
ChangeHistoryService:SetWaypoint("Scripts cleaning")
for i, element in pairs(SelectedInstance:GetDescendants()) do
if element:IsA("BaseScript") then
element:Destroy()
end
end
ChangeHistoryService:SetWaypoint("Scripts cleaned")
end
end
CleanScriptsButton.Click:Connect(CleanScriptsButtonClicked)
Here is the plugin created:
Issue:
When i use Undo or Ctrl+Z. the scripts don’t get recovered!
In the wrong place, it’s within the first for loop
local function CleanScriptsButtonClicked()
local Selection = SelectionService:Get()
for i, SelectedInstance in pairs(Selection) do
ChangeHistoryService:SetWaypoint("Scripts cleaning")
for i, element in pairs(SelectedInstance:GetDescendants()) do
if element:IsA("BaseScript") then
element:Destroy()
end
end
end
ChangeHistoryService:SetWaypoint("Scripts cleaned")
--I fixed it
end
@IlyasTawawe
I’ve actually ran into this issue before. :Destroy 'd objects can not be reverted with ChangeHistoryService. (Due to their parent property being locked) You have to set the parent to nil for the history service to work. (Which isn’t optimal, but studio internally uses this method to allow un-deleting objects)
Also, make sure you put the first waypoint outside of the loop.
Edit (Code example):
local SelectionService = game:GetService("Selection")
local ChangeHistoryService = game:GetService("ChangeHistoryService")
local Toolbar = plugin:CreateToolbar("ToolboxCleaner")
local CleanScriptsButton = Toolbar:CreateButton("CleanScripts", "Select a model to clean all it's descendant scripts", "rbxassetid://3610247188")
local function CleanScriptsButtonClicked()
local Selection = SelectionService:Get()
ChangeHistoryService:SetWaypoint("Scripts cleaning")
for i, SelectedInstance in pairs(Selection) do
for i, element in pairs(SelectedInstance:GetDescendants()) do
if element:IsA("BaseScript") then
element.Parent = nil --Ugly but required
end
end
ChangeHistoryService:SetWaypoint("Scripts cleaned")
end
end
CleanScriptsButton.Click:Connect(CleanScriptsButtonClicked)
Thank you so much everyone for the kind help! @Dolphin_Worm@ThatTimothy@TypicalHB
It will probably fix it. ChangeHistoryService was always a pain for me to work with.
I’ll probably make a video about it in the video so other people know such simple tips that can save hours of debugging!
EDIT: It worked! I appreciate it