This is glitching out and no one knows why

So I’m trying to make a part selection thing but it is glitching out and I don’t know why its flashing disappearing and reappearing and I do not know why please tell me why!

here is the code

local HttpService = game:GetService("HttpService")
local Players = game:GetService("Players")
local LocalPlayer = Players.LocalPlayer
local Mouse = LocalPlayer:GetMouse()
local Character = LocalPlayer.Character or LocalPlayer.CharacterAdded:Wait()
local HumanoidRootPart = Character:WaitForChild("HumanoidRootPart")

local Distance = 15

Mouse.Move:Connect(function()
	if Mouse.Target and math.round((HumanoidRootPart.Position - Mouse.Target.Position).Magnitude) < Distance then
		local PreviousMouseTarget = Mouse.Target
		wait()
		if Mouse.Target == PreviousMouseTarget and not Mouse.Target:FindFirstChildOfClass("SelectionBox") then
			local SelectionBox = Instance.new("SelectionBox")
			SelectionBox.Color3 = Color3.new(0, 0, 0)
			SelectionBox.LineThickness = 0.01
			SelectionBox.Adornee = Mouse.Target
			SelectionBox.Name = HttpService:GenerateGUID()
			SelectionBox.Parent = Mouse.Target
		else
			local PreviousSelectionBox = PreviousMouseTarget:FindFirstChildOfClass("SelectionBox")
			PreviousSelectionBox:Destroy()
		end
	end
end)

Mouse.Button1Down:Connect(function()
	if Mouse.Target and math.round((HumanoidRootPart.Position - Mouse.Target.Position).Magnitude) < Distance then
		Mouse.Target:Destroy()
	end
end)

pls heklp me

1 Like

that is your problem

local HttpService = game:GetService("HttpService")
local Players = game:GetService("Players")
local LocalPlayer = Players.LocalPlayer
local Mouse = LocalPlayer:GetMouse()
local Character = LocalPlayer.Character or LocalPlayer.CharacterAdded:Wait()
local HumanoidRootPart = Character:WaitForChild("HumanoidRootPart")
local Distance = 15
local PreviousMouseTarget = nil

Mouse.Move:Connect(function()
    if Mouse.Target and math.round((HumanoidRootPart.Position - Mouse.Target.Position).Magnitude) < Distance then
        wait()
        if Mouse.Target ~= PreviousMouseTarget and PreviourMouseTarget ~= nil and PreviousMouseTarget:FindFirstChildOfClass("SelectionBox") then
            PreviousMouseTarget.SelectionBox:Destroy()
        end
        if Mouse.Target ~= PreviousMouseTarget and PreviourMouseTarget ~= nil and not PreviousMouseTarget:FindFirstChildOfClass("SelectionBox") then
            PreviousMouseTarget = Mouse.Target
            local SelectionBox = Instance.new("SelectionBox")
            SelectionBox.Color3 = Color3.new(0, 0, 0)
            SelectionBox.LineThickness = 0.01
            SelectionBox.Adornee = Mouse.Target
            SelectionBox.Name = HttpService:GenerateGUID()
            SelectionBox.Parent = Mouse.Target
        end
    end
end)

Mouse.Button1Down:Connect(function()
    if Mouse.Target and math.round((HumanoidRootPart.Position - Mouse.Target.Position).Magnitude) < Distance then
        Mouse.Target:Destroy()
    end
end)

No. The selection box does not show up at all now

I should of ‘fixed’ the script in studio instead of the dev forum text thing, sorry about that

local HttpService = game:GetService("HttpService")
local Players = game:GetService("Players")
local LocalPlayer = Players.LocalPlayer
local Mouse = LocalPlayer:GetMouse()
local Character = LocalPlayer.Character or LocalPlayer.CharacterAdded:Wait()
local HumanoidRootPart = Character:WaitForChild("HumanoidRootPart")

local Distance = 15

local PreviousMouseTarget = Mouse.Target

Mouse.Move:Connect(function()
	if Mouse.Target and math.round((HumanoidRootPart.Position - Mouse.Target.Position).Magnitude) < Distance then
        wait()
        if Mouse.Target ~= PreviousMouseTarget and PreviousMouseTarget ~= nil and PreviousMouseTarget:FindFirstChildOfClass("SelectionBox") then
            PreviousMouseTarget.SelectionBox:Destroy()
        end
		if Mouse.Target ~= PreviousMouseTarget and PreviousMouseTarget ~= nil and not PreviousMouseTarget:FindFirstChildOfClass("SelectionBox") then
			PreviousMouseTarget = Mouse.Target
			local SelectionBox = Instance.new("SelectionBox")
			SelectionBox.Color3 = Color3.new(0, 0, 0)
			SelectionBox.LineThickness = 0.01
			SelectionBox.Adornee = Mouse.Target
			SelectionBox.Visible = true
			SelectionBox.Parent = Mouse.Target
		end
	end
end)

Mouse.Button1Down:Connect(function()
	if Mouse.Target and math.round((HumanoidRootPart.Position - Mouse.Target.Position).Magnitude) < Distance then
		Mouse.Target:Destroy()
	end
end)

you did not fix it againssssssss

STILL NEED HELP WITH THiS PLEASE HELP ME

here IS my CODE so far:

local RunService = game:GetService("RunService")
local HttpService = game:GetService("HttpService")
local Players = game:GetService("Players")
local LocalPlayer = Players.LocalPlayer
local GetMouse = LocalPlayer:GetMouse()
local LocalPlayerCharacter = LocalPlayer.Character or LocalPlayer.CharacterAdded:Wait()
local LocalPlayerHumanoidRootPart = LocalPlayerCharacter:WaitForChild("HumanoidRootPart")

local Distance = 15
local ComponentTable = {}

RunService.RenderStepped:Connect(function()
	local RayNew = Ray.new(GetMouse.UnitRay.Origin, GetMouse.UnitRay.Direction * 100000)
	local Component, Position, SurfaceNormal, Material = workspace:FindPartOnRayWithIgnoreList(RayNew, {LocalPlayerCharacter})
	if Component and not Component:FindFirstChildOfClass("SelectionBox") then
		local SelectionBox = Instance.new("SelectionBox")
		SelectionBox.Color3 = Color3.new(0, 0, 0)
		SelectionBox.LineThickness = 0.01
		SelectionBox.Adornee = Component
		SelectionBox.Name = HttpService:GenerateGUID()
		SelectionBox.Parent = Component
		if not table.find(ComponentTable, Component.Name) then
			table.insert(ComponentTable, Component.Name)
		end
	end
	wait()
	if ComponentTable[#ComponentTable - 1] ~= nil and Component.Name ~= ComponentTable[#ComponentTable - 1] then
		local PreviousComponent = workspace.Terrain:FindFirstChild(ComponentTable[#ComponentTable - 1])
		if PreviousComponent then
			local PreviousSelectionBox = PreviousComponent:FindFirstChildOfClass("SelectionBox")
			if PreviousSelectionBox then
				PreviousSelectionBox:Destroy()
			end
		end
	end
end)

Im getting desperate please

Solution:

local RunService = game:GetService("RunService")
local HttpService = game:GetService("HttpService")
local Players = game:GetService("Players")
local LocalPlayer = Players.LocalPlayer
local GetMouse = LocalPlayer:GetMouse()

local PreviousMouseTarget = GetMouse.Target

RunService.RenderStepped:Connect(function()
	if PreviousMouseTarget ~= nil and GetMouse.Target == PreviousMouseTarget and not GetMouse.Target:FindFirstChildOfClass("SelectionBox") then
		local SelectionBox = Instance.new("SelectionBox")
		SelectionBox.Color3 = Color3.new(0, 0, 0)
		SelectionBox.LineThickness = 0.01
		SelectionBox.Adornee = GetMouse.Target
		SelectionBox.Name = HttpService:GenerateGUID()
		SelectionBox.Parent = GetMouse.Target
	end
	if PreviousMouseTarget ~= nil and GetMouse.Target ~= PreviousMouseTarget and PreviousMouseTarget:FindFirstChildOfClass("SelectionBox") then
		PreviousMouseTarget:FindFirstChildOfClass("SelectionBox"):Destroy()
	end
	PreviousMouseTarget = GetMouse.Target
end)