Hi, I recently made a select script for my tool. However I keep getting these outputs. How do I fix it?
code
--Variable
local localplayer = game:GetService("Players").LocalPlayer
local mouse = localplayer:GetMouse()
local uis = game:GetService("UserInputService")
local tool = script.Parent
local Debounce = false
on = false
tool.Equipped:Connect(function() -- when player equips tool, it create the selection
on = true
local highlight = Instance.new("Highlight")
highlight.Name = "Selection"
highlight.Parent = workspace
highlight.FillTransparency = 1
highlight.Adornee = nil
end)
tool.Unequipped:Connect(function()
on = false
local highlight = workspace:FindFirstChild("Selection")
highlight:Destroy()
end)
mouse.Move:Connect(function()
if mouse.Target:IsA("BasePart") and mouse.Target ~= nil then
if mouse.Target.Locked == false then
local ignorelist = mouse.TargetFilter == workspace
local highlight = workspace:WaitForChild("Selection")
highlight.Adornee = mouse.Target
end
end
end)
the output
17:41:28.511 Players.LiminalP.Backpack.MassTool.LocalScript:23: attempt to index nil with 'IsA' - Client - LocalScript:23
18:48:09.386 Infinite yield possible on 'Workspace:WaitForChild("Selection")' - Studio
well the IsA error is happening because mouse.Target is returning nil, and you’re checking if its nil after calling it. (Just swap the two around)
the highlight issue is because its not finding the highlight in workspace. You should instead create a reference outside of the scopes like so:
-- put this variable at the top of the script:
local highlight = nil
-- for creating it
highlight = Instance.new("Highlight")
-- destroying
if highlight then
highlight:Destroy()
end
You’re calling WaitForChild() per every mouse.Move Event, and if Selection isn’t found inside the workspace then it’ll keep infinitely yielding until the selected item was found
You should use the Timeout Parameter that WaitForChild() provides you with, where if the specified item doesn’t exist after searching for X seconds, then it returns back nil
local highlight = workspace:WaitForChild("Selection", 1)
if highlight ~= nil then -- "highlight" can return the Instance, or nil so we want to implement a sanity check for this
highlight.Adornee = mouse.Target
end