Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
2

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

Asked by 4 years ago

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

Log in to vote
2
Answered by 4 years ago
Edited 4 years ago

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)
Ad

Answer this question