MoveTo()'error'ing

Hey developers, I’ve been coming across a error recently, or more specifically: Unable to cast value to Object. Now I am not familiar to this error, and haven’t got it mostly.

The objective for this script is to make the player move randomly, here it is:

script.Parent.Touched:Connect(function(part)
	if part.Parent.Humanoid then
    local char = part.Parent
	local hum = char.Humanoid
	local x = char.HumanoidRootPart.Position.X
	local y = char.HumanoidRootPart.Position.Y
	local z= char.HumanoidRootPart.Position.Z

	while wait() do
		hum:MoveTo(math.random(x, x+5),y,math.random(z, z+5)) --Error Line 
	end
end

Any answers to this topic would be greatly appreciated.

Thanks.

:MoveTo takes a Vector3, not 3 numbers.

hum:MoveTo(Vector3.new(math.random(x, x + 5), y, math.random(z, z + 5)))

By the way you are trying to move the humanoid 30 times a second, do you really want to do this? You would essentially be overwriting the goal position 30 times a second.

1 Like

Added a wait, so it shouldn’t move 30 times a second, and yes.

But wait() yields the thread for ~1/30th of a second.

So it will.

Are you absolutely sure this is what you want? I am pretty sure what you would want would be moving around, then waiting for the first move to finish so you can move again.

That would probably be better, any knowledge on how to actually execute that?

while true do
    hum:MoveTo(Vector3.new(math.random(x, x + 5), y, math.random(z, z + 5)))
    hum.MoveToFinished:Wait()
end

Turns out it was my mistake, I should of said ‘Lose Control’ instead of ‘Move Randomly’, by waiting for the move to finish, if the player is walking it wouldn’t activate, thanks for the help anyway.