Error is being spammed in console but the script works fine

Hey there! I’m wondering how I would avoid this error, as it gets annoying because it gets spammed:

If I try to add a check, the script may not work properly. However, the script works fine, even with the error being spammed.
The line in question: local pos1 = mouse.Target.Position

Here is the part of the script causing the issue:

coroutine.wrap(function()
			render = game:GetService("RunService").RenderStepped:Connect(function()
				local pos1 = mouse.Target.Position
				local pos2 = character:WaitForChild("HumanoidRootPart").Position
				local magnitude = (pos1 - pos2).magnitude
				if magnitude <= 40 then
					blockIsPlaceable = false
					selection.Visible = true
				else
					blockIsPlaceable = true
					selection.Visible = false
				end
			end)
		end)()

Thanks!

Just do this:

render = game:GetService("RunService").RenderStepped:Connect(function()
local pos1 = character.PrimaryPart.Position
local pos2 = mouse.Target.Position
local magnitude = (pos1 - pos2).Magnitude
if magnitude <= 40 then
					blockIsPlaceable = false
					selection.Visible = true
				else
					blockIsPlaceable = true
					selection.Visible = false
				end
          end)

It is useless to use coroutine in this case due to the fact RunService.RenderStepped is a thread (I believe so)

1 Like

Thank you! I didn’t realize that.

Do you still have the Issue or is this solved?

I still have the error, as it doesn’t solve the Mouse.Target situation though, but it did help improve the script, thanks for that.

Try doing Mouse.Hit.p, might work better
(probably not)

The error doesn’t show up, but you can’t actually place blocks or do anything like that anymore. Thanks for the suggestion though!

ok so, after looking at the script, use pcall(),

Documentation on pcall

render = game:GetService("RunService").RenderStepped:Connect(function()
pcall(function()
local pos1 = character.PrimaryPart.Position
local pos2 = mouse.Target.Position
local magnitude = (pos1 - pos2).Magnitude
if magnitude <= 40 then
					blockIsPlaceable = false
					selection.Visible = true
				else
					blockIsPlaceable = true
					selection.Visible = false
				end
                    end)
          end)

I tried that too. It kinda worked, but that made other things in my script error (e.g. because mouse.Target is null, the magnitude line errors)

EDIT: Oh, I get what you mean. I was only doing one line haha, I’ll try it now

Can’t you check if mouse.Target is nil or not and then do something based off that

Yeah, that’s why I tried to avoid checks as much as possible. Thanks for your suggestion though!

Thank you so much! It works perfectly.

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