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.1)
			selectionBox.Color3 = Color3.new(0, 1, 0)
			debounce = false
		end
	end
end

local function onEquipped ()
	isEquipped = true
	while isEquipped do
		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.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)

5 comments

0

It seems fine to me. Are you having performance problems?

0

no, i just wanted it to be as best as possible, since this is a vital part of the game

0

while loop in onEquipped function is quite inneficient, you better use the UserInputService.InputChanged and then check if Input.UserInputType == Enum.UserInputType.MouseMovement which fires when you move the mouse, then you check the target.

1

Here is example, this is the most efficient way: https://pastebin.com/JXUDRvP8

0

Thankyou!