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

MoveTo() not working and model teleporting above player or not moving at all?

I'm building a model from scratch, not really sure how to animate it too well, but I'm really more interested in making an AI for it.

It's supposed to move to a player location, but the model does nothing. Whenever I put just the model (dummy:MoveTo(target.Position)) it moves to above the player since the player is in the player position and it can't be there. Is this because I don't have any animations on my model? If I were to add an animation would it walk there instead of teleporting above the player/at player position?

function findPlayer(dummyPosition)
	local dist = 1000
	local torso = nil
	local tempDist = dist
	local minDist = dist
	for i, player in pairs(game.Players:GetPlayers()) do
		if (player ~= nil) then
			local tempTorso = player.Character:findFirstChild("HumanoidRootPart")
			tempDist = ((tempTorso.Position - dummyPosition).magnitude)
			if ((tempDist)< dist)then
				if (tempDist < minDist) then
					torso = tempTorso
					minDist = tempDist
				end
			end
		end

	end
	
	return torso
	
end


while true do
	wait(1)
	local target = findPlayer(script.Parent.HumanoidRootPart.Position)
	if target ~= nil then
		print(target.Position)
		dummy.Humanoid:MoveTo(target.Position)
	end

end

1 answer

Annoyingly, this is how setting the Position property works. It will look upwards until it finds a suitable position -- in this case, right above the player.


So, a burning question lurks from deep within your confused cranium...

how do I fix this?

The answer is simple: Use Coordinate Frame, or CFrame for short. If a model has a PrimaryPart, you can use the Model:SetPrimaryPartCFrame(cf) method.

What is a CFrame?

Read up on it here. The Wiki is your goto for all things Roblox. Plus, it explains it better than I possibly could.

1 comment

0

Why exactly is the MoveTo() not working? Is it because of the model having no animation? I haven't even played Roblox. I've spent two days in the Roblox Studio. If I set a primary part with CFrames, would that allow my MoveTo() function to work? Or would I have to manually get it to glide to it's desired location?

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