Hi, I'm trying to create a part with a mesh of a snake. I want this snake to then follow any users within the magnitude of 40 studs and to continue doing so until they've left the magnitude. - For example. They died.
However, at this point in time, the snake won't move and I can't seem to work out why...
while wait(.1) do for i,v in pairs(workspace:GetChildren()) do if v:IsA("Model") then x = v:FindFirstChild("Humanoid") if x then c = v:FindFirstChild("dontcome") --This is inside certain players to prevent snakes chasing them... if c then else z = x.Parent:FindFirstChild("Head") if z then if x.Health < 1 then else if (script.Parent.Head.Position - z.Position).Magnitude <= 40 then script.Parent.Humanoid:MoveTo(z.Position) else wait(0.01) end end end end end end end end
Everything inside the "Snake":
https://gyazo.com/5de683e395307fd7f43f8064aa6d86b2
There are no errors in the output either.
Thank you.
Ok, so because your snake is just one part, I'm assuming it's trying to get a full character rig to walk to the point, which it can't detect. So my solution is to place a BodyPosition into the Head, and have the body position move to the z.Position.
Edit2: So I went back to my original answer and changed it up a bit so it would search through players only to decrease the time for each loop to finish, and for some reason that also fixes the problem of it not working when you reset.
repeat wait() until game.Players.ChildAdded wait() while true do wait() for _, v in pairs(game.Players:GetChildren()) do local plr = v.Character if plr.Humanoid.Health > 0 then local head = plr.Head if math.abs((head.Position-script.Parent.Head.Position).Magnitude) >= 40 then script.Parent.Head.BodyPosition.Position = head.Position end end end end
while true do wait() for _, v in pairs(workspace:GetChildren()) do if v.ClassName == "Model" and v:FindFirstChild("HumanoidRootPart") then local plr = v if script.Parent.Humanoid.Health > 0 then local head = plr.Head if math.abs(math.abs(head.Position).Magnitude)-(math.abs(script.Parent.Head.Position)).Magnitude) < 41 then script.Parent.Humanoid.WalkToPoint = Vector3.new(head.Position) end end end end end
Easier to understand, the math.abs is the absolute value, if the players heads magnitude is less than or equal to 40 to the snake then the snake moves to the head. Don't forget to add vectors.
I am not willing to read what the guy above wrote... But this is how I would do it.
Alright there, edited again.