[SOLVED]Need help with Mouse.Target "attempt to index nil with 'Parent' ", huge performance bug

  1. What do you want to achieve? Keep it simple and clear!

So recently, I have implemented this plot selection system that semi works as of the moment.
I’m trying to make it so that when the player equips a tool (i.e a box of seeds), the cursor will be able to highlight a plot of soil to a bright green color that the player wishes to plant seeds in.
When the player unequips the tool, the plot selection system is disabled and the plots return to their natural color until the tool is equipped again.

  1. What is the issue? Include screenshots / videos if possible!

The main problem is that when equipping/unequipping the tool, the mouse.target system is left on somehow, and when the player places their cursor in the sky, it issues this error:

This is a HUGE problem, especially for performance as the more plots i place with my shovel tool,
the more checks it has to do that are left over somehow, which greatly increases the lag every time I place my cursor into the skybox.

  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I have looked for many solutions on the forums and other sites like Scripting Helpers, however
    even after adding checks like:
-- if Mouse.Target ~= nil then
---- blah blah blah...

The computer still returns the same error.
I think it has something to do with my for loops, i’m not too sure anymore as I lose confidence every time this error comes back.

Here’s the entirety of the script:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local carrotseeds = script.Parent
local Player = game.Players.LocalPlayer
local mouse = Player:GetMouse()

carrotseeds.Equipped:Connect(function()
	
	if mouse.TargetFilter ~= nil then
		mouse.TargetFilter = nil
	end
	
	mouse.Move:Connect(function()
		local target = mouse.Target
		

		if target.Parent == workspace.Plots and mouse.Target ~= nil then
			target.BrickColor = BrickColor.new("Lime green")
		elseif target.Parent ~= workspace.Plots and mouse.Target ~= nil then
			for i, v in pairs(workspace.Plots:GetChildren())do
				v.BrickColor = BrickColor.new("Reddish brown")
			end
		end
		
		
	end)
	mouse.Idle:Connect(function()
		local target = mouse.Target
		
		if target.Parent == workspace.Plots and mouse.Target ~= nil then
			target.BrickColor = BrickColor.new("Lime green")
		elseif target.Parent ~= workspace.Plots and mouse.Target ~= nil then
			for i, v in pairs(workspace.Plots:GetChildren())do
				v.BrickColor = BrickColor.new("Reddish brown")
			end
		elseif target == nil then
			return
		end
		
	end)	
end)
carrotseeds.Unequipped:Connect(function()
	if mouse.TargetFilter == nil then
		mouse.TargetFilter = workspace.Plots
	end
	if mouse.Target == nil then
		return true
	end
	
end)



Any help and feedback would be greatly appreciated. Thank you!

Okay, nevermind!
I just had to simplify my conditionals within the mouse functions a little bit and restructure them with the following:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local carrotseeds = script.Parent
local Player = game.Players.LocalPlayer
local mouse = Player:GetMouse()

carrotseeds.Equipped:Connect(function()
	
	if mouse.TargetFilter ~= nil then
		mouse.TargetFilter = nil
	end
	
	mouse.Move:Connect(function()
		local target = mouse.Target
		

		if target and target.Name == "Plot" then
			target.BrickColor = BrickColor.new("Lime green")
		elseif target and target.Name ~= "Plot" then
			for i, v in pairs(workspace.Plots:GetChildren())do
				v.BrickColor = BrickColor.new("Reddish brown")
			end
		end
		
		
	end)
	mouse.Idle:Connect(function()
		local target = mouse.Target
		
		if target and target.Name == "Plot" then
			target.BrickColor = BrickColor.new("Lime green")
		elseif target and target.Name ~= "Plot" then
			for i, v in pairs(workspace.Plots:GetChildren())do
				v.BrickColor = BrickColor.new("Reddish brown")
			end
		elseif target == nil then
			return
		end
		
	end)	
end)
carrotseeds.Unequipped:Connect(function()
	if mouse.TargetFilter == nil then
		mouse.TargetFilter = workspace.Plots
	end
end)

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.