local delete = game:GetService("ReplicatedStorage"):WaitForChild("Delete")
local player = game:GetService("Players").LocalPlayer
local mouse = player:GetMouse()
local selectionBox = Instance.new("SelectionBox")
selectionBox.Color3 = Color3.new(0, 1, 0)
selectionBox.LineThickness = 0.01
selectionBox.Parent = player.PlayerGui
local debounce = false
local isEquipped
local target
local function onActivated ()
if debounce == false then
if target and target.Name == "Deletable" and player:DistanceFromCharacter(target.Position) < 18 then
debounce = true
delete:FireServer(target)
target:Destroy()
selectionBox.Color3 = Color3.new(1, 0, 0)
target = mouse.Target
if target and target.Name == "Deletable" and player:DistanceFromCharacter(target.Position) < 18 then
selectionBox.Adornee = target
else
selectionBox.Adornee = nil
end
wait(0.2)
selectionBox.Color3 = Color3.new(0, 1, 0)
debounce = false
end
end
end
local function onEquipped ()
print("Equipped!")
isEquipped = true
while isEquipped do
target = mouse.Target
print("Checking!")
if target and target.Name == "Deletable" and player:DistanceFromCharacter(target.Position) < 18 then
print("Setting adornee!")
selectionBox.Adornee = target
print(selectionBox.Parent.Name)
else
selectionBox.Adornee = nil
end
wait(0.1)
end
end
local function onUnequipped ()
isEquipped = false
selectionBox.Adornee = nil
end
script.Parent.Activated:Connect(onActivated)
script.Parent.Equipped:Connect(onEquipped)
script.Parent.Unequipped:Connect(onUnequipped)
This is a local script placed in a tool in the starterpack. The script works normally with expected results at first (Parent is PlayerGui), But after resetting the character, the parent of the selectionBox becomes nil.
Any ideas why this is happening?

1 comment
Update: It works after setting the parent of selectionBox to workspace, but I want it to be in the player PlayerGui