Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Moving part to player within magnitude help?

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.

2 comments

0

Also, I don't have the item inside my character called "dontcome". So thats not affecting it..

0

I think I got it now

2 answers

✓ Accepted answer

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

1 comment

0

Sorry but this code doesn't work..

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.

5 comments

0

Sorry but the code above doesn't work..

0

does it move if you just say script.Parent.Humanoid:MoveTo(Vector3.new(head.Position)) ?

0

you really overused math.abs, so much to the point of adding extra parenthesis because you couldn't see what was going on

0

also MoveTo doesn't work for an object with the hierarchy specified

0

Alright then WalkToPoint is a way around it.

This discussion is preserved as it was. New answers, comments, and votes are closed.