What do you want to achieve? Keep it simple and clear!
I want to know why the selection box isn’t getting destroyed after using both :remove() and :destroy()
What is the issue? Include screenshots / videos if possible!
The selection box that is created when the player character gets the tool doesn’t get destroyed.
What solutions have you tried so far? Did you look for solutions on the Developer Hub?
I’ve tried putting it on a folder then deleting the folder alongside the selection box however it is still the same result whether with a folder or without.
script below:
--this is in a server script--
changeVal.OnServerEvent:Connect(function(plr, hbOn)
for i, p in pairs(game:GetService("Players"):GetPlayers()) do
local character = p.Character or p.CharacterAdded:Wait()
local default = character:WaitForChild("Default")
local newHitbox = Instance.new("SelectionBox")
newHitbox.Name = "NewHitbox"
--repeat wait()
--until default
if hbOn == true then
wait()
if not default then return end
if default then
-- do stuff
end
else
if hbOn == false then
-- revert stuff
end
end
end
end)
Every time the OnServerEvent is fired you are looping through all the players and adding a NEW selectionbox to the player then in your if statement you are setting said selectionbox to nil and destroying it, instead of referencing any previous selectionboxes already added.
Honestly, I would just re-write your code like this so you don’t have to keep creating/deleting the same selection box.
Heres a more simplified version of script (may have typos)
--this is in a server script--
local PS = game:GetService("Players");
changeVal.OnServerEvent:Connect(function(PlayerTriggered, Status)
for _, Player in ipairs(PS:GetPlayers()) do
local Character = Player.Character or Player.CharacterAdded:Wait();
local Default = Character:WaitForChild("Default");
if Default then -- You really don't need this since your using WaitForChild()
local Hitbox = Default:FindFirstChild("Hitbox"); -- If it doesn't exist, it will return nil
if not Hitbox then -- Did not find a hitbox withingDoesn't exist so we create one
Hitbox = Instance.new("SelectionBox"); -- if it didn't find the hitbox before then it creates new one for the player
Hitbox.Name = "NewHitbox";
Hitbox.LineThickness = 0.025;
Hitbox.Color3 = Color3.new(0.364706, 0, 1);
Hitbox.Adornee = Default.Handle;
Hitbox.Parent = Default;
end;
-- Now we just toggle the visibility which is much better then just creating and destroying constantly
Hitbox.Visible = Status; -- We set it to status to toggle between true and false depending on what client sends
end;
end;
end);