basically, i have a loop that moves a certain part to a certain enemy. pretty simple. now theres a bit of a glitch with this: the first while loop works fine… but the next one just ignores everything and doesnt run the “moving the part” code. here’s the script:
while slashstormmainpart ~= nil do
print("starting loop")
local smallestdistance = 70
local targetpart
for i, v in pairs(workspace.zombiesworkspace:GetChildren()) do
task.wait()
if v:FindFirstChild("HumanoidRootPart") ~= nil and v:FindFirstChildOfClass("Humanoid") ~= nil and game.Players:GetPlayerFromCharacter(v) == nil then
print("maybe found")
if (v.HumanoidRootPart.Position - slashstormmainpart.Position).Magnitude < smallestdistance and v:FindFirstChildOfClass("Humanoid").Health > 0 then
print("whoa found")
smallestdistance = (v.HumanoidRootPart.Position - slashstormmainpart.Position).Magnitude
targetpart = v.HumanoidRootPart
end
end
end
if targetpart ~= nil then
local timetomove = smallestdistance/4
print(timetomove)
for i = 0.01, timetomove, .01 do
print(i/timetomove)
slashstormmainpart.Position = slashstormmainpart.Position:Lerp(targetpart.Position, i/timetomove + 0.05)
slashstormmainpart.CFrame = CFrame.lookAt(slashstormmainpart.Position, targetpart.Position) * CFrame.Angles(math.pi, 0, 0)
task.wait(.01)
end
else
print("no targetpart")
end
print("ending loop")
task.wait(.4)
end
the first time it runs it fully prints the “i/timetomove” thingy buut the next time it just spams
“starting loop” “maybe found” “no targetpart” “ending loop”
Instead of printing some words after an if check, print the variables checked inside the if before it for better troubleshooting. It lets you know why an if is doing something unexpected.
Something like:
print("x = ", x, " y = ", y) -- tells you what's going on with the variables
if x < y then
print("it worked") -- doesn't tell you why it's NOT working
-- code
end
Then you know where to look at the variables to see why they aren't working.
oh, it seems that… for some reason after it finishes the for i = 0.01 loop it just goes to the coordinates 0, -3, 0. welp i think ill be able to fix that. probably. maybe.
i think i found the issue, the positions given to cframe.lookat are the same, which causes it to go ASTRONOMICALLY far away. i can just switch the places of these lines and it should work alright. slashstormmainpart.Position = slashstormmainpart.Position:Lerp(targetpart.Position, i/timetomove + 0.05) slashstormmainpart.CFrame = CFrame.lookAt(slashstormmainpart.Position, targetpart.Position) * CFrame.Angles(math.pi, 0, 0)
for i = 0.01, timetomove, .01 do
print(i/timetomove)
if slashstormmainpart.Position == targetpart.Position then
slashstormmainpart.Position = slashstormmainpart.Position + Vector3.new(0, 1, 0)
end
slashstormmainpart.CFrame = CFrame.lookAt(slashstormmainpart.Position, targetpart.Position) * CFrame.Angles(math.pi, 0, 0)
slashstormmainpart.Position = slashstormmainpart.Position:Lerp(targetpart.Position, i/timetomove + 0.05)
if i == timetomove then
slashstormmainpart.Position = targetpart.Position
end
task.wait(.01)
end