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!
DasKairo
(Cairo)
November 9, 2022, 6:05am
#2
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.
DasKairo
(Cairo)
November 9, 2022, 6:12am
#4
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.
DasKairo
(Cairo)
November 9, 2022, 6:13am
#6
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!
DasKairo
(Cairo)
November 9, 2022, 6:18am
#8
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!
DasKairo:
ok so, after looking at the script, use pcall()
,
Documentation on pcall
Lua Globals | Roblox Creator Documentation
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)
Thank you so much! It works perfectly.
system
(system)
Closed
November 23, 2022, 6:24am
#12
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.