Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How to make this script which puts a border on the targeted block print no errors?

I made a script that will put a border around what your mouse is pointing at it works but it prints hundreds of errors when I'm pointing at the sky

Here is the script

-- Get Player
local Player = game.Players.LocalPlayer

-- MainThings
local TileGrid = workspace.Tiles
local Mouse = Player:GetMouse()

-- Border
local Border = Instance.new("SelectionBox")
Border.Parent = workspace
Border.LineThickness = .01
Border.Color3 = Color3.new(255,255,255)

-- Services
local RunService = game:GetService("RunService")

RunService.Heartbeat:Connect(function()
	local Target = Mouse.Target
	if Target.Parent == TileGrid then
		Border.Adornee = Target
	else
		Border.Adornee = nil
	end
end)

1 answer

✓ Accepted answer

Hello.


Problem:

  • You did not check if the target was existent. A target is nil when you're pointing to the sky.

Solution:

  • Add an if statement checking if the mouse target is existent.

Recommendation(s):

  • Use Mouse.Move instead of RunService.Heartbeat to prevent lag.

Fixed Script:

-- Get Player
local Player = game.Players.LocalPlayer

-- MainThings
local TileGrid = workspace.Tiles
local Mouse = Player:GetMouse()

-- Border
local Border = Instance.new("SelectionBox")
Border.Parent = workspace
Border.LineThickness = .01
Border.Color3 = Color3.new(255,255,255)

-- Event
Mouse.Move:Connect(function()
    local Target = Mouse.Target
	if Target then
	    if Target.Parent == TileGrid then
	        Border.Adornee = Target
	    else
	        Border.Adornee = nil
	    end
	end
end)

This discussion is preserved as it was. New answers, comments, and votes are closed.