Yup. No duplicate parts in the same position… all the parts are anchored, too. Oops. when i tried to press edit to clarify that the “loop result:” print, printed the same position as the part we are trying to avoid, i accidentally clicked “solution”. I hope that didn’t confuse you guys.
You’re writing redundant code and it’s difficult to follow along.
if Exclude and v.Position == Exclude.Position then
Exlude
here is an Instance, thus it will always return true. So this check is unnecessary. It can also cause unexpected behaviour if Exclude
happens to be nil, which is what I suspect is happening (print Exclude
just to make sure).
Just check if the part is excluded and skip it if it is:
if v == Exclude then
print("They're the same...")
continue
end
if NearestWalkPoint then
PointMagnitude = (RootPart.Position - v.Position).Magnitude
if PointMagnitude < NearestPointMagnitude then
NearestWalkPoint = v
NearestPointMagnitude = PointMagnitude
end
else
NearestWalkPoint = v
NearestPointMagnitude = (RootPart.Position - v.Position).Magnitude
end
Yeah, I added the extra stuff because I already tried exactly what you did. Sorry, that’s my bad, I should’ve communicated that better. I’ll try exactly what you said again, but and I’ll tell you what the exclude variable prints. Thanks!
The first time the function is called, Exclude prints as nil. This is to be expected-- as “Last Target” does not exist yet.
The second time the function is called, Exclude prints as “BearWalkPart”. I selected the printed part, and it was indeed the target walk point we don’t want to include (just to make sure, I then selected every other walkpoint to make sure there was no duplicates). Although, the code seems tell me “lol idc” and continues to use the Exclude variable in the calculations anyways. Here are the relevant prints, I’ll explain what they mean
“They’re the same…” - What prints if the if statement comparing Exclude passes
“Loop Result:” – For clarification on the following 2 prints
“BearWalkPart” – The name of the instance that passed the Exclude comparison if statement
First Pos Value – What the “NearestWalkPoint” position is in the loop
Second Pos Value – What the “Target” position is outside of the function (it has +3.5 so that the bear seems to walk with its feet on the ground, and not in the ground).
Here’s the function code:
local function BearPosclosestToPlayer(Exclude)
local NearestWalkPoint, NearestPointMagnitude
local PointMagnitude
for i, v in BearWalkPointFolders do
if v == Exclude then
print("They're the same...")
continue
end
if NearestWalkPoint then
PointMagnitude = (RootPart.Position - v.Position).Magnitude
if PointMagnitude < NearestPointMagnitude then
NearestWalkPoint = v
NearestPointMagnitude = PointMagnitude
end
else
NearestWalkPoint = v
NearestPointMagnitude = (RootPart.Position - v.Position).Magnitude
end
end
print("Loop Result:")
print(Exclude)
print(NearestWalkPoint.Position)
return NearestWalkPoint.Position
end
EDIT: sorry, this is an important clarification! I use “BearWalkPointFolders”, as this is an early variable that presents itself in the code earlier than what I showed that just means “workspace.BearWalkPointFolders:GetChildren()”, “BearWalkPointFolders” plural does not mean there are multiple folders. ALSO, I don’t use a table variable right now as if I overlooped the past table that already has the Exlcude variable taken out of it, it’d likely not even print as the variable in the table wasn’t even there to begin with. This won’t fix the problem; as the code before the presented solution already used “Table” as an argument and it didn’t fix the problem. Just to be sure, I’ll test the “Table” argument.
Yup. As I suspected, going back with the “Table” argument still didn’t solve the issue. Interestingly-- despite prints disagreeing-- the Table argument still holds BearWalkPart and passes through the Exclude comparison-- which, previous prints say it Table doesn’t hold BearWalkPoint. Hm.
PROBLEM SOLVED! HOORAY!
I was debugging for a while, until I decided to grab a pencil and paper and write down everything I actually knew. I realized, that I didn’t actually know if the Last Target was the same as the actual last target. So, I ran some prints. I realized that Last Target was actually being “randomly” selected-- as the code thought every instance in AvailableFolder was the same-- so statements like “if LastTarget == Exclude” actually worked for any part in the table, meaning that the first part randomly selected in the loop would just be skipped. This also explains why one of my bug tests let my bear finish two walk points instead of just one; as I was lucky and the first walk point selected was indeed the last target.
How’d I fix this?
I changed the code so that instead of comparing instances I compare the positions of the parts selected. It was really that easy, lol. The bear works perfect now. Thank you to everyone that helped, I am very grateful! Happy Thanksgiving!
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.