Humanoid:MoveTo() Doesn't work on non human shaped rig

In my game I have this firefly model:

And I wanted to have it randomly fly around using the Humanoid’s :MoveTo() function.
But for some reason, no matter what I try it won’t move.

So far I have tried messing around with the HipHeight property (which didn’t seem to change anything), unanchored every part, and I also tried TweenService and CFrame:Lerp() (These kind of worked, but if it interpolates slow enough it start’s to jitter)

I have two scripts in here:
A ServerScript in the humanoid with the Legacy run context

----Variables:

local MapBounds = {Vector2.new(20,20),Vector2.new(100,100)} --x,y = x,z in 3d
local MaxHeight	= 100
local object = script.Parent.Parent:WaitForChild("HumanoidRootPart")
local humanoid = script.Parent

local target
local look = Vector3.new(0, 0, 0)
local moving = false

local RunService = game:GetService("RunService")

local TweenService = game:GetService("TweenService")

----Functions:

local bv = Instance.new("BodyVelocity")
bv.Velocity = Vector3.new(0,0,0)
bv.MaxForce = Vector3.new(90,math.huge,90)--Play around with this
bv.P = 9000--Play around with this
bv.Parent = object

object.CFrame = CFrame.new(math.random(-50, 50), object.CFrame.Position.Y, math.random(-50, 50))

target = object.CFrame

local function getHeightAtPoint(position : Vector2)
	local Pos3D  = Vector3.new(position.X,MaxHeight,position.Y)
	
	local params = RaycastParams.new()
	local result = workspace:Raycast(Pos3D,Vector3.new(0,-1,0)*(MaxHeight*1.1), params)

	if result and result.Position then
		return result.Position.Y
	end
end

local function getRandomPosition()
	local randX  = math.random(MapBounds[1].X,MapBounds[2].X)
	local randZ  = math.random(MapBounds[1].Y,MapBounds[2].Y)
	
	--local height = getHeightAtPoint(Vector2.new(randX,randZ))
	local height = math.random(4, 6)

	if not height then error("no height found") end 
	
	target = CFrame.new(randX,height,randZ)
	look = Vector3.new(randX,height,randZ)
	return Vector3.new(randX,height,randZ)
end

--[[
RunService.Heartbeat:Connect(function(dt)
	if moving then
		object.CFrame = object.CFrame:Lerp(CFrame.lookAt(object.Position, look), 0.1)
	end
end)

local MoveTween = TweenService:Create(object, TweenInfo.new(1 * (object.Position - look).magnitude, Enum.EasingStyle.Cubic, Enum.EasingDirection.InOut), {
	Position = look
})

MoveTween:Play()
MoveTween.Completed:Connect(function()
	moving = false

	wait(math.random(1, 5))

	getRandomPosition()
	
	MoveTween:Play()
	moving = true
end)
]]

humanoid:MoveTo(getRandomPosition())
humanoid.MoveToFinished:Connect(function(reached)
	wait(math.random(1, 5))
	humanoid:MoveTo(getRandomPosition())
end)

(The stuff in the multiline comment is the tween service, I disabled that part because of the aforementioned jittering)

And another script called Animate with the Client Run Context which makes it hover up and down in place, and also plays an animation of the wings fluttering:

local WingFlutterAnim = script.Parent.Animation.WingFlutter
local WingFlutterTrack = script.Parent.Humanoid.Animator:LoadAnimation(WingFlutterAnim)

WingFlutterTrack:Play()

local RunService = game:GetService("RunService") -- define RunService, to make lerps (alternative version of tweens) work you will need to run them in a loop

local object = script.Parent:WaitForChild("Torso") -- this is your coin bag, it won't work if it's a model, however, I can modify the script so it works with models, just tell me

local speed = 2
local distance = 0.4 
local smoothness = 0.1 -- from 0 to 1, the closer it is to 0 the smoother it will be

RunService.Heartbeat:Connect(function() -- this function will run in a loop
	local t = tick() -- time
	local y = math.sin(t * speed) * distance -- this is a trigonometric function we will use to get the offset
	
	object.CFrame = object.CFrame:Lerp((object.CFrame * CFrame.new(0, y, 0)), smoothness) -- now just set the cframe
end)

For models, you would want to use TweenService and not :MoveTo(). :MoveTo() is for Characters and not Models. Hope this helps.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.